ReflectionClass inspects the definition of a class, rather than running its constructor or calling its methods.
What Is ReflectionClass::getAttributes in PHP?
Returns reflection descriptors for attributes declared on the class, optionally filtered by attribute name.
ReflectionClass::getAttributes(?string $name = ..., int $flags = ...) : array
Parameters:
$name— Optional. The exact class-member name (method, property or constant) to inspect.$flags— Optional. A set of documented option bits controlling how the operation behaves.
Return type: array. The return value contains ReflectionAttribute descriptors. To construct an attribute object, call newInstance() on a descriptor.
Example 1: Understanding the Basic Behavior
<?php
#[Attribute(Attribute::TARGET_CLASS)]
class Route { public function __construct(public string $path) {} }
#[Route("/products")]
class GuideController {}
$reflectionClass = new ReflectionClass(GuideController::class);
$attributes = $reflectionClass->getAttributes(Route::class);
foreach ($attributes as $attribute) {
echo $attribute->newInstance()->path . "\n";
}
Output:
/products
Explanation: Returns reflection descriptors for attributes declared on the class, optionally filtered by attribute name.
Example 2: Testing Another Case
<?php
#[Attribute(Attribute::TARGET_CLASS)]
class Route { public function __construct(public string $path) {} }
#[Route("/orders")]
class GuideController {}
$reflectionClass = new ReflectionClass(GuideController::class);
$attributes = $reflectionClass->getAttributes(Route::class);
foreach ($attributes as $attribute) {
echo $attribute->newInstance()->path . "\n";
}
Output:
/orders
Explanation: The return value contains ReflectionAttribute descriptors. To construct an attribute object, call newInstance() on a descriptor.
Practical Example: Applying the Operation
<?php
#[Attribute(Attribute::TARGET_CLASS)]
class Route { public function __construct(public string $path) {} }
#[Route("/reports")]
class GuideController {}
$reflectionClass = new ReflectionClass(GuideController::class);
$attributes = $reflectionClass->getAttributes(Route::class);
foreach ($attributes as $attribute) {
echo $attribute->newInstance()->path . "\n";
}
Output:
/reports
Explanation: The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Important Behavior and Edge Cases
- The return value contains ReflectionAttribute descriptors. To construct an attribute object, call newInstance() on a descriptor.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionClass::getAttributes | Returns reflection descriptors for attributes declared on the class, optionally filtered by attribute name. |
ReflectionClass::getDocComment() | getAttributes() returns structured attribute descriptors; getDocComment() reads unstructured PHPDoc text. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Treating metadata returned by ReflectionClass::getAttributes() as though it executes the reflected class member. | The return value contains ReflectionAttribute descriptors. To construct an attribute object, call newInstance() on a descriptor. |
| Choosing ReflectionClass::getDocComment() without checking whether it provides the same result. | getAttributes() returns structured attribute descriptors; getDocComment() reads unstructured PHPDoc text. |
Key Takeaways
- Returns reflection descriptors for attributes declared on the class, optionally filtered by attribute name.
- The return value contains ReflectionAttribute descriptors. To construct an attribute object, call newInstance() on a descriptor.
- The returned metadata may be false, null, an array, or a Reflection object depending on the requested property.
The examples demonstrate where ReflectionClass::getAttributes fits within reflection and metadata. Verify the relevant preconditions whenever you adapt the code to real data.