Content · Programming · PHP

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

Checks whether a specific enum case name exists.

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

When building tools that inspect PHP code, ReflectionEnum::hasCase() helps answer a focused question: checks whether a specific enum case name exists.

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

What Is ReflectionEnum::hasCase()?


Checks whether a specific enum case name exists.

Its method signature is:

public function hasCase(string $name): bool

Parameters:

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

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->hasCase("Pickup");
var_dump($result);

Output:

bool(true)

Explanation: Checks whether a specific enum case name exists.

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->hasCase("Missing");
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: The check is by case name, not by backing 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);

$result = $enumReflection->hasCase("Delivery");
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: getCase returns its ReflectionEnumUnitCase when it exists.

Important Behavior and Edge Cases


  • The check is by case name, not by backing value.
  • getCase returns its ReflectionEnumUnitCase when it exists.
  • 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::hasCase()Checks whether a specific enum case name exists.
Related APIgetCase returns its ReflectionEnumUnitCase when it exists.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: The check is by case name, not by backing value.Handle the specific limitation: The check is by case name, not by backing value.
Confusing this method with Related APIgetCase returns its ReflectionEnumUnitCase when it exists.

Key Takeaways


  • Checks whether a specific enum case name exists.
  • The check is by case name, not by backing value.
  • getCase returns its ReflectionEnumUnitCase when it exists.

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