New reflection methods for working with attributes
| From: | Ollie Read | Date: | Tue, 25 Jul 2023 18:09:17 +0000 |
| Subject: | New reflection methods for working with attributes | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-120863@lists.php.net to get a copy of this message | ||
Hello all,
A while back, I wrote a lengthy post about suggested improvements for reflection, but I would like
to come back to address additional methods for dealing with attributes. (I have an open git issue
here: https://github.com/php/php-src/issues/8489)
I'd like to introduce the following three methods on all reflect classes that have attributes:
hasAttribute(string $name, int $flags = 0): bool
Uses the same filtering as getAttributes(), but returns true if a match is found, false otherwise.
getAttribute(string $name, int $flags = 0): ?ReflectionAttribute
Also uses the same filtering as getAttributes(), except that it will return an instance of
ReflectionAttribute if one is found, null if none are found, and will throw an exception of more
than 1 is found.
getNumberOfAttributes(?string $name = null, int $flags = 0): int
Again, uses the same filtering as getAttributes(), and is to attributes what getNumberOfParameters()
is to parameters. I appreciate that this methods use may not be obvious, but I wanted to include it
for consistency.
I'm relatively confident that I have worked out the best method of implementing these, and
contributors have agreed to merge a PR, which I will start shortly, should there be no objections.
---
Best Regards,
*Ollie Read*