Content · Programming · PHP

PHP ReflectionEnumBackedCase::getBackingValue() Method: Usage and Examples

Reads the integer or string backing value assigned to an enum case.

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

When building tools that inspect PHP code, ReflectionEnumBackedCase::getBackingValue() helps answer a focused question: reads the integer or string backing value assigned to an enum case.

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

What Is ReflectionEnumBackedCase::getBackingValue()?


Reads the integer or string backing value assigned to an enum case.

Its method signature is:

public function getBackingValue(): string|int

This method has no required arguments.

Return value: string|int

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->getBackingValue();
var_dump($result);

Output:

string(6) "pickup"

Explanation: Reads the integer or string backing value assigned to an 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(ShippingMethod::class);
$caseReflection = (new ReflectionEnum(ShippingMethod::class))->getCase("Delivery");

$result = $caseReflection->getBackingValue();
var_dump($result);

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

Output:

string(8) "delivery"

Result type: string

Explanation: This method applies only to backed enum cases, not pure enum cases.

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("Pickup");

$result = $caseReflection->getBackingValue();
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:

string(6) "pickup"

Reflection result available

Explanation: getValue returns the enum case object itself.

Important Behavior and Edge Cases


  • This method applies only to backed enum cases, not pure enum cases.
  • getValue returns the enum case object itself.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionEnumBackedCase::getBackingValue()Reads the integer or string backing value assigned to an enum case.
Related APIgetValue returns the enum case object itself.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: This method applies only to backed enum cases, not pure enum cases.Handle the specific limitation: This method applies only to backed enum cases, not pure enum cases.
Confusing this method with Related APIgetValue returns the enum case object itself.

Key Takeaways


  • Reads the integer or string backing value assigned to an enum case.
  • This method applies only to backed enum cases, not pure enum cases.
  • getValue returns the enum case object itself.

Use ReflectionEnumBackedCase::getBackingValue() 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.