When building tools that inspect PHP code, ReflectionClassConstant::getAttributes() helps answer a focused question: lists PHP attributes attached to the selected class constant.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionClassConstant::getAttributes()?
Lists PHP attributes attached to the selected class constant.
Its method signature is:
public function getAttributes(?string $name = ..., int $flags = ...): array
Parameters:
$name— Name of the method, case, property, constant, extension, or parameter being requested May be omitted.$flags— Filtering behavior such as ReflectionAttribute::IS_INSTANCEOF May be omitted.
Return value: array
Example 1: Inspect a typed constant
Start with a self-contained PHP declaration and inspect the method result.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
class ConstantLabel { public function __construct(public string $value) {} }
class ReportLabels {
/** Human-readable identifier for exported reports. */
#[ConstantLabel('export')]
final public const string KIND = 'csv';
protected const int RETENTION_DAYS = 30;
private const bool INTERNAL = true;
}
enum DeliveryStage: string { case Queued = 'queued'; case Sent = 'sent'; }
$constantReflection = new ReflectionClassConstant(ReportLabels::class, "KIND");
$result = $constantReflection->getAttributes();
echo count($result);
Output:
1
Explanation: Lists PHP attributes attached to the selected class constant.
Example 2: Compare a constant with different visibility
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
class ConstantLabel { public function __construct(public string $value) {} }
class ReportLabels {
/** Human-readable identifier for exported reports. */
#[ConstantLabel('export')]
final public const string KIND = 'csv';
protected const int RETENTION_DAYS = 30;
private const bool INTERNAL = true;
}
enum DeliveryStage: string { case Queued = 'queued'; case Sent = 'sent'; }
$constantReflection = new ReflectionClassConstant(ReportLabels::class, "RETENTION_DAYS");
$result = $constantReflection->getAttributes();
echo count($result);
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
0
Result type: array
Explanation: Without matching attributes the result is an empty array, not null.
Practical Example: Inspect another declared constant
This final case shows how the same API can be used when inspecting application definitions.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_CLASS_CONSTANT)]
class ConstantLabel { public function __construct(public string $value) {} }
class ReportLabels {
/** Human-readable identifier for exported reports. */
#[ConstantLabel('export')]
final public const string KIND = 'csv';
protected const int RETENTION_DAYS = 30;
private const bool INTERNAL = true;
}
enum DeliveryStage: string { case Queued = 'queued'; case Sent = 'sent'; }
$constantReflection = new ReflectionClassConstant(ReportLabels::class, "INTERNAL");
$result = $constantReflection->getAttributes();
echo count($result);
// Use the reflection result in a simple decision.
if (is_bool($result)) {
echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
echo "\nEntries: " . count($result);
} elseif ($result === null) {
echo "\nNo value returned";
} else {
echo "\nReflection result available";
}
Output:
0
Entries: 0
Explanation: ReflectionAttribute::newInstance instantiates one selected attribute.
Important Behavior and Edge Cases
- Without matching attributes the result is an empty array, not null.
- ReflectionAttribute::newInstance instantiates one selected attribute.
- Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
ReflectionClassConstant::getAttributes() | Lists PHP attributes attached to the selected class constant. |
Related API | ReflectionAttribute::newInstance instantiates one selected attribute. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Without matching attributes the result is an empty array, not null. | Handle the specific limitation: Without matching attributes the result is an empty array, not null. |
| Confusing this method with Related API | ReflectionAttribute::newInstance instantiates one selected attribute. |
Key Takeaways
- Lists PHP attributes attached to the selected class constant.
- Without matching attributes the result is an empty array, not null.
- ReflectionAttribute::newInstance instantiates one selected attribute.
Use ReflectionClassConstant::getAttributes() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.