Skip to content

Valid attribute on methods break when read via ReflectionFunction #8982

Closed
@nicolas-grekas

Description

@nicolas-grekas

Description

The following code:

#[Attribute(Attribute::TARGET_METHOD)]
class A
{
}

class B
{
    #[A]
    public function m()
    {
    }
}

$a = new B();
$c = [$a, 'm'];
$r = new ReflectionFunction($c(...));
var_dump($r->getAttributes()[0]->newInstance());

Resulted in this output:

Fatal error: Uncaught Error: Attribute "A" cannot target function (allowed targets: method)

But I expected this output instead:

object(A)#5 (0) {
}

PHP Version

PHP >= 8.1.6

Operating System

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions