When building tools that inspect PHP code, ReflectionEnum::isBacked() helps answer a focused question: distinguishes string/int-backed enums from pure enums.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionEnum::isBacked()?
Distinguishes string/int-backed enums from pure enums.
Its method signature is:
public function isBacked(): bool
This method has no required arguments.
Return value: bool
Example 1: Inspect a backed enum
Start with a self-contained PHP declaration and inspect the method result.
<?php
declare(strict_types=1);
enum ShippingMethod: string { case Pickup = 'pickup'; case Delivery = 'delivery'; }
enum Progress { case Waiting; case Finished; }
$enumReflection = new ReflectionEnum(ShippingMethod::class);
$result = $enumReflection->isBacked();
var_dump($result);
Output:
bool(true)
Explanation: Distinguishes string/int-backed enums from pure enums.
Example 2: Compare with a pure enum or alternate case
Next, change the reflected declaration or parameter to observe a related situation.
<?php
declare(strict_types=1);
enum ShippingMethod: string { case Pickup = 'pickup'; case Delivery = 'delivery'; }
enum Progress { case Waiting; case Finished; }
$enumReflection = new ReflectionEnum(Progress::class);
$result = $enumReflection->isBacked();
var_dump($result);
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
bool(false)
Result type: bool
Explanation: Pure enum cases have names but no backing values.
Practical Example: Use enum metadata in domain code
This final case shows how the same API can be used when inspecting application definitions.
<?php
declare(strict_types=1);
enum ShippingMethod: string { case Pickup = 'pickup'; case Delivery = 'delivery'; }
enum Progress { case Waiting; case Finished; }
$enumReflection = new ReflectionEnum(ShippingMethod::class);
$result = $enumReflection->isBacked();
var_dump($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:
bool(true)
Status: yes
Explanation: getBackingType returns the corresponding scalar type or null.
Important Behavior and Edge Cases
- Pure enum cases have names but no backing values.
- getBackingType returns the corresponding scalar type or null.
- 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 |
|---|---|
ReflectionEnum::isBacked() | Distinguishes string/int-backed enums from pure enums. |
Related API | getBackingType returns the corresponding scalar type or null. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Pure enum cases have names but no backing values. | Handle the specific limitation: Pure enum cases have names but no backing values. |
| Confusing this method with Related API | getBackingType returns the corresponding scalar type or null. |
Key Takeaways
- Distinguishes string/int-backed enums from pure enums.
- Pure enum cases have names but no backing values.
- getBackingType returns the corresponding scalar type or null.
Use ReflectionEnum::isBacked() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.