When building tools that inspect PHP code, ReflectionEnumUnitCase::getValue() helps answer a focused question: returns the enum case instance represented by this reflection.
Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.
What Is ReflectionEnumUnitCase::getValue()?
Returns the enum case instance represented by this reflection.
Its method signature is:
public function getValue(): UnitEnum
This method has no required arguments.
Return value: UnitEnum
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);
$caseReflection = (new ReflectionEnum(ShippingMethod::class))->getCase("Pickup");
$result = $caseReflection->getValue();
echo $result->name;
Output:
Pickup
Explanation: Returns the enum case instance represented by this reflection.
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);
$caseReflection = (new ReflectionEnum(Progress::class))->getCase("Waiting");
$result = $caseReflection->getValue();
echo $result->name;
// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);
Output:
Waiting
Result type: Progress
Explanation: An enum instance is not the same as the backed scalar value.
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);
$caseReflection = (new ReflectionEnum(ShippingMethod::class))->getCase("Delivery");
$result = $caseReflection->getValue();
echo $result->name;
// 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:
Delivery
Reflection result available
Explanation: ReflectionEnumBackedCase::getBackingValue returns a string or integer.
Important Behavior and Edge Cases
- An enum instance is not the same as the backed scalar value.
- ReflectionEnumBackedCase::getBackingValue returns a string or integer.
- 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 |
|---|---|
ReflectionEnumUnitCase::getValue() | Returns the enum case instance represented by this reflection. |
Related API | ReflectionEnumBackedCase::getBackingValue returns a string or integer. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring this condition: An enum instance is not the same as the backed scalar value. | Handle the specific limitation: An enum instance is not the same as the backed scalar value. |
| Confusing this method with Related API | ReflectionEnumBackedCase::getBackingValue returns a string or integer. |
Key Takeaways
- Returns the enum case instance represented by this reflection.
- An enum instance is not the same as the backed scalar value.
- ReflectionEnumBackedCase::getBackingValue returns a string or integer.
Use ReflectionEnumUnitCase::getValue() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.