When building tools that inspect PHP code, ReflectionEnum::getBackingType() helps answer a focused question: returns the backed enum's declared scalar type as ReflectionNamedType.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionEnum::getBackingType()?
Returns the backed enum's declared scalar type as ReflectionNamedType.
Its method signature is:
public function getBackingType(): ?ReflectionNamedType
This method has no required arguments.
Return value: ?ReflectionNamedType
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->getBackingType();
echo $result?->getName() ?? "pure enum";
Output:
string
Explanation: Returns the backed enum's declared scalar type as ReflectionNamedType.
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->getBackingType();
echo $result?->getName() ?? "pure enum";
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
pure enum
Result type: null
Explanation: Pure enums return null because they have no string or integer backing type.
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->getBackingType();
echo $result?->getName() ?? "pure enum";
// 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:
string
Reflection result available
Explanation: isBacked tells whether the enum has a backing type.
Important Behavior and Edge Cases
- Pure enums return null because they have no string or integer backing type.
- isBacked tells whether the enum has a backing type.
- 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::getBackingType() | Returns the backed enum's declared scalar type as ReflectionNamedType. |
Related API | isBacked tells whether the enum has a backing type. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: Pure enums return null because they have no string or integer backing type. | Handle the specific limitation: Pure enums return null because they have no string or integer backing type. |
| Confusing this method with Related API | isBacked tells whether the enum has a backing type. |
Key Takeaways
- Returns the backed enum's declared scalar type as ReflectionNamedType.
- Pure enums return null because they have no string or integer backing type.
- isBacked tells whether the enum has a backing type.
Use ReflectionEnum::getBackingType() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.