Content · Programming · PHP

PHP ReflectionEnumUnitCase::getEnum() Method: Usage and Examples

Returns reflection for the enum that declares the selected case.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

When building tools that inspect PHP code, ReflectionEnumUnitCase::getEnum() helps answer a focused question: returns reflection for the enum that declares the selected case.

Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.

What Is ReflectionEnumUnitCase::getEnum()?


Returns reflection for the enum that declares the selected case.

Its method signature is:

public function getEnum(): ReflectionEnum

This method has no required arguments.

Return value: ReflectionEnum

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->getEnum();
echo $result->getName();

Output:

ShippingMethod

Explanation: Returns reflection for the enum that declares the selected case.

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->getEnum();
echo $result->getName();

// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);

Output:

Progress
Result type: ReflectionEnum

Explanation: It returns ReflectionEnum, not an enum case instance.

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->getEnum();
echo $result->getName();

// 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:

ShippingMethod
Reflection result available

Explanation: getValue returns the actual enum case instance.

Important Behavior and Edge Cases


  • It returns ReflectionEnum, not an enum case instance.
  • getValue returns the actual enum case instance.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionEnumUnitCase::getEnum()Returns reflection for the enum that declares the selected case.
Related APIgetValue returns the actual enum case instance.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: It returns ReflectionEnum, not an enum case instance.Handle the specific limitation: It returns ReflectionEnum, not an enum case instance.
Confusing this method with Related APIgetValue returns the actual enum case instance.

Key Takeaways


  • Returns reflection for the enum that declares the selected case.
  • It returns ReflectionEnum, not an enum case instance.
  • getValue returns the actual enum case instance.

Use ReflectionEnumUnitCase::getEnum() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.

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