Skip to content

Language: ReflectionAttribute::newInstance() — instantiate attribute objects (ext/reflection parity) #3206

Description

@PurHur

Category

language | stdlib

Problem

PHP 8 attributes compile and read via ReflectionClass::getAttributes() + ReflectionAttribute::getName() (#1936 ✅). Zend also supports ReflectionAttribute::newInstance() to construct the attribute class with constructor args from the declaration. Frameworks (DI, routing, validation) depend on newInstance() — without it, attributes are names-only metadata.

#2467 tracks JIT/AOT reflection tables; this issue is the VM semantic for newInstance().

php-src reference

Repro (failure today)

<?php
#[Attribute]
class Route {
    public function __construct(public string $path) {}
}
#[Route('/home')]
class C {}
$a = (new ReflectionClass(C::class))->getAttributes()[0];
$o = $a->newInstance();
echo $o->path;
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'

Zend PHP: /home
This compiler: fatal (method missing) or wrong behavior

Scope (this repo)

  • Store attribute constructor arg expressions on ClassEntry / method metadata (from php-cfg attrGroups)
  • lib/VM/Builtin/ReflectionAttributeGetName.php → add newInstance method
  • lib/VM/BuiltinClasses.php — register ReflectionAttribute::newInstance
  • Evaluate attribute class new with compile-time constant args v1; reject dynamic args with clear error
  • test/compliance/cases/language/attribute_new_instance.phpt
  • Language: PHP 8 attributes reflection JIT/AOT (phase 2, post-#1354 VM) #2467 for JIT mirror after VM green

Done when

  • Repro prints /home on VM
  • Repeated newInstance() returns new object instances (Zend semantics)
  • Invalid/missing attribute class → ReflectionException matching Zend wording
  • ./script/ci-fast.sh --filter attribute_new_instance green

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions