Content · Programming · PHP

PHP ReflectionEnum::getCase() Method: Usage and Examples

Returns reflection for one named enum case.

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

When building tools that inspect PHP code, ReflectionEnum::getCase() helps answer a focused question: returns reflection for one named enum case.

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

What Is ReflectionEnum::getCase()?


Returns reflection for one named enum case.

Its method signature is:

public function getCase(string $name): ReflectionEnumUnitCase

Parameters:

  • $name — Name of the method, case, property, constant, extension, or parameter being requested Required.

Return value: ReflectionEnumUnitCase

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->getCase("Pickup");
echo $result->getName();

Output:

Pickup

Explanation: Returns reflection for one named enum 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);

$result = $enumReflection->getCase("Waiting");
echo $result->getName();

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

Output:

Waiting
Result type: ReflectionEnumUnitCase

Explanation: An invalid case name raises ReflectionException.

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->getCase("Delivery");
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:

Delivery
Reflection result available

Explanation: getCases returns reflections for all cases.

Important Behavior and Edge Cases


  • An invalid case name raises ReflectionException.
  • getCases returns reflections for all cases.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionEnum::getCase()Returns reflection for one named enum case.
Related APIgetCases returns reflections for all cases.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: An invalid case name raises ReflectionException.Handle the specific limitation: An invalid case name raises ReflectionException.
Confusing this method with Related APIgetCases returns reflections for all cases.

Key Takeaways


  • Returns reflection for one named enum case.
  • An invalid case name raises ReflectionException.
  • getCases returns reflections for all cases.

Use ReflectionEnum::getCase() 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.