Hi internals,
It is common attributes to be used as markers. Just like a marker interface with no methods, such attributes don’t have any parameter.
Examples from Symfony:
- #[Ignore] indicating that a property should be skipped in a serialization contextl
- #[Exclude] telling the dependency injection container that a given class should be skipped from automatic service registration
From Doctrine ORM:
- #[Id] to tell which property of en entity class should be mapped as primary key
Those are meant to opt in/out some behavior through detection only. But with the current API, such code needs to call getAttributes()
while it doesn't care about the returned ReflectionAttribute instances.
Hence I’m wondering if we could add a hasAttribute()
method to the reflectors that are targetable by attributes.
Does that make sense?
If it does, do I need to open an RFC for that feature or is a pull request be enough?
Anyway, I’d be happy to work on the implementation as a first contribution to PHP.
Looking forward to your feedback.
Thanks.
Robin
Robin Chalas
Principal Engineer @Les-Tilleuls.coop
03 66 72 43 94
robin@les-tilleuls.coop
82 Rue Winston Churchill - 59160 Lomme
Hi internals,
It is common attributes to be used as markers. Just like a marker
interface with no methods, such attributes don’t have any parameter.Examples from Symfony:
- #[Ignore] indicating that a property should be skipped in a
serialization contextl- #[Exclude] telling the dependency injection container that a given
class should be skipped from automatic service registrationFrom Doctrine ORM:
- #[Id] to tell which property of en entity class should be mapped as
primary keyThose are meant to opt in/out some behavior through detection only. But
with the current API, such code needs to callgetAttributes()
while
it doesn't care about the returned ReflectionAttribute instances.
Hence I’m wondering if we could add ahasAttribute()
method to the
reflectors that are targetable by attributes.Does that make sense?
If it does, do I need to open an RFC for that feature or is a pull
request be enough?Anyway, I’d be happy to work on the implementation as a first
contribution to PHP.
Looking forward to your feedback.Thanks.
Robin
I think that would require an RFC.
To clarify, you're proposing to replace this:
count((new ReflectionClass(Beep::class))->getAttributes(Ignore::class)) == true
with
(new ReflectionClass(Beep::class))->hasAttribute(Ignore::class)
Right? That doesn't seem like a huge improvement.
--Larry Garfield
I think that would require an RFC.
To clarify, you're proposing to replace this:
count((new ReflectionClass(Beep::class))->getAttributes(Ignore::class)) ==
truewith
(new ReflectionClass(Beep::class))->hasAttribute(Ignore::class)
Right? That doesn't seem like a huge improvement.
--Larry Garfield
Actually,
if ((new ReflectionClass(Beep::class))->getAttributes(Ignore::class))
would work as well, so basically:
$rc->hasAttribute(Ignore::class) === (bool)
$rc->getAttributes(Ignore::class);
- Benjamin