Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Attrbiute on intrerface #4427

Open
Norbytus opened this issue Jan 30, 2025 · 1 comment
Open

Use Attrbiute on intrerface #4427

Norbytus opened this issue Jan 30, 2025 · 1 comment

Comments

@Norbytus
Copy link

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?

@damianwadley
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants