[php-src] Issue #8489: Add additional methods for reflections that have attributes
| From: | ollieread | Date: | Tue, 03 May 2022 14:24:26 +0000 |
| Subject: | [php-src] Issue #8489: Add additional methods for reflections that have attributes | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-241406@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8489
Author: ollieread
### Description
All currently attributable reflections have a single method relating to attributes.
```
getAttributes(?string $name = null, int $flags = 0): array
```
Additional methods for dealing with attributes would be a nice improvement and would simplify common
approaches. I'm proposing the following methods.
## getAttribute
Get a single instance of
ReflectionAttribute, following the same rules as
getAttributes, minus the fact that $name is required.
```
getAttribute(string $name, int $flags = 0): ?ReflectionAttribute
```
If more than one attribute matches, an exception should be thrown.
## hasAttribute
Functionally identical to getAttribute, except it will return true if one
or more matches are found, otherwise it returns false.
```
hasAttribute(string $name, int $flags = 0): bool
```
## getNumberOfAttributes
Again, follows the same rules as getAttributes, but will return the number found. This
particular method is based on the [ReflectionFunctionAbstract::getNumberOfParameters
method](https://www.php.net/manual/en/reflectionfunctionabstract.getnumberofparameters.php).
```
getNumberOfAttributes(?string $name = null, int $flags = 0): int
```