php8.5
Home/ Manual/ reflection / reflectionconstant/ ReflectionConstant::getAttributes

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

VersionDescription
8.5.0This 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::getAttributes
  • ReflectionClassConstant::getAttributes
  • ReflectionFunctionAbstract::getAttributes
  • ReflectionProperty::getAttributes

Source: reference/reflection/reflectionconstant/getattributes.xml · from the official PHP manual (php/doc-en)