ReflectionConstant::getAttributes
PHP function
Edit on GitHub ✎
Gets Attributes
Description
ReflectionConstant::getAttributes(string|null $name = null, int $flags = 0): array
Returns all attributes declared on this global constant as an array of ReflectionAttribute.
Parameters
Return Values
Array of attributes, as ReflectionAttribute objects.
Changelog
| Version | Description |
|---|---|
| 8.5.0 | This method was introduced. |
Examples
Basic usage
php
<?php
#[Attribute]
class Fruit {
}
#[Attribute]
class Red {
}
#[Fruit]
#[Red]
const APPLE = 'apple';
$constant = new ReflectionConstant('APPLE');
$attributes = $constant->getAttributes();
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
?>The above example will output:
output
Array
(
[0] => Fruit
[1] => Red
)See Also
ReflectionClass::getAttributesReflectionClassConstant::getAttributesReflectionFunctionAbstract::getAttributesReflectionProperty::getAttributes