Skip to content

Language: PHP 8 attributes (#[Attr] on classes, methods, properties) #1354

@PurHur

Description

@PurHur

Problem

PHP 8 attributes (#[Route('/home')], #[\AllowDynamicProperties]) appear in modern frameworks and on paths toward self-host (vendor/php-cfg may carry attribute nodes). The compiler today does not lower attributes — lint may reject decorated declarations or php-cfg may drop metadata silently.

Wave 3 finish line #1751 tracks landing this with #1360, #1366, #1378.

Goal (minimum viable v1)

  1. Parse attribute groups on classes, methods, properties, parameters, constants (host php-parser already tokenizes on PHP 8.2+ — Dependencies: php-parser token map for PHP 8.2+ host runs #113).
  2. Store attribute AST on CFG/class metadata (or attach to Compiler symbol table).
  3. Runtime: ignore attributes for execution (PHP allows unused attributes) OR expose read-only via future reflection stub.
  4. Lint: remove UnsupportedRegistry block when VM accepts decorated declarations.

Defer: JIT/AOT attribute reflection, compile-time #[\Attribute] validation, enum attribute targets.

Implementation hints

Layer Files Notes
Parser vendor/nikic/php-parser AttributeGroup, Attribute nodes on PHP 8.2 host
CFG php-cfg / lib/Compiler.php Preserve attribute list on Class_, ClassMethod, Property
VM lib/VM.php Strip before dispatch or store on class metadata bag
Lint lib/Lint/UnsupportedRegistry.php Map until VM lands
Self-host SelfHostBuiltinPolicy No-op if vendor uses attributes only for static analysis
Docs docs/capabilities-syntax.md Regenerate via php script/capability-syntax.php

Tests

  • test/compliance/cases/attribute_class.phpt — class with #[\AllowDynamicProperties] instantiates
  • test/compliance/cases/attribute_method.phpt — method attribute ignored at runtime
  • test/unit/AttributeParseTest.php — lint green on decorated stub

Unblock order

  1. Host parser tokens (Dependencies: php-parser token map for PHP 8.2+ host runs #113 if 8.1 host fails)
  2. CFG carry-through in Compiler::compileClass
  3. VM ignore semantics + compliance PHPT
  4. JIT: document deferral in capability matrix (like intersection types Language: Intersection types (A&B) in parameters and properties #1357)

Acceptance criteria

./phpc lint -r '#[\AllowDynamicProperties] class C {}'
./phpc run -r '#[\AllowDynamicProperties] class C {} new C(); echo "ok";'
./script/ci-fast.sh --filter Attribute

Docker:

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  ./script/ci-fast.sh --filter Attribute

No GitHub Actions.

Dependencies

Links

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions