doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

Use Attrbiute on intrerface

Open Norbytus opened this issue 1 year ago • 1 comments

I don't find any documentation about using Attribute on interfaces

Example:

<?php

#[\Attribute]
final readonly class TestAttribute
{}

#[TestAttribute]
interface TestInterface {}

final readonly Test implements TestInterface {}

// If i try get class attribute with reflection
$t = new Test;
$r = new \ReflectionClass($t);
echo count($r->getAttributes()); // Display 0

And its valid using or not?

Norbytus avatar Jan 30 '25 15:01 Norbytus

Attributes are not inherited, so if you define TestAttribute on the interface then you must call getAttributes on that interface directly. You can, of course, use methods like getParentClass and getInterfaces to "discover" all attributes of a class. https://3v4l.org/A1a7q

I do not see this mentioned in the documentation. Do you have a suggestion on how to add it?

damianwadley avatar Jan 30 '25 17:01 damianwadley