Content · Web Development · PHP

PHP ReflectionClass::getAttributes() Method: Usage and Examples

PHP ReflectionClass::getAttributes() Method: Usage and Examples: Returns reflection descriptors for attributes declared on the class, optionally filtered by attribute name.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

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


ApproachWhen to Use It
ReflectionClass::getAttributesReturns 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


MistakeCorrect 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.

Share this resourceSend this page to someone who may find it useful.