How do I get all values of an enum in PHP?

How do I get all values of an enum in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How do I get all values of an enum in PHP?

PHP 8.1 introduced native support for enumerations, a feature that has quickly become a staple for defining fixed sets of constants within a class structure. While the syntax for declaring an enum is straightforward, developers often hit a wall when they need to iterate over every possible value defined in that type. Unlike standard constants or arrays, you cannot simply access them via global scope once an enum exists; instead, you must leverage the specific reflection capabilities built into the language.

The most robust and idiomatic way to retrieve all values of an enum is by using ReflectionEnum. This class provides a dedicated method, getCases(), which returns an array containing every case defined for that specific enum. This approach works seamlessly regardless of whether the enum uses positional values or string-based names, making it the preferred solution over manual inspection or type-guessing.

Consider a scenario where you are building a payment processor and need to handle every possible transaction status. You might define your statuses like this:

enum TransactionStatus: string
{
    case Pending = 'pending';
    case Approved = 'approved';
    case Declined = 'declined';
}

To iterate through these values, you can instantiate a reflection object for the specific enum class and call getCases():

$reflection = new ReflectionEnum(TransactionStatus::class);
$cases = $reflection->getCases();

foreach ($cases as $case) {
    // Access the name (e.g., 'Pending') or value ('pending')
    echo $case->getName() . ': ' . $case->getValue() . PHP_EOL;
}

This output will list all three statuses. The ReflectionEnum instance also allows you to access specific cases directly if you know the name, providing a powerful mix of iteration and direct access patterns.

A common pitfall when working with enums in early versions or specific contexts is attempting to iterate over them using get_defined_constants(). While tempting because enums behave like constants, this function does not return enum cases by default unless they are declared as global constants (which they are not). Relying on get_defined_constants() for enums will result in an empty array, leading to silent logic errors in your application. Always prefer the reflection API when dealing with user-defined enumerations.

Another consideration is performance. While ReflectionEnum adds a slight overhead due to introspection, it is negligible in most application contexts where you are iterating over a small fixed set of values. However, if you find yourself calling this operation inside a tight loop millions of times per request, you might consider caching the list of cases or storing them as a static array property within your enum logic.

For frameworks like Laravel, there is often a need to validate incoming data against all possible enum values dynamically. You can use the retrieved cases to build an allowlist for validation rules:

$allowedValues = array_map(
    fn ($case) => $case->getValue(), 
    (new ReflectionEnum(TransactionStatus::class))->getCases()
);

This ensures your validation logic stays in sync with your enum definitions, preventing the need to manually update arrays whenever you add a new case.

If you are working within an enterprise PHP environment, maintaining consistency across teams is crucial. Using ReflectionEnum ensures that your code remains agnostic to how the underlying values were defined—whether they are integers or strings—and provides a single source of truth for discovering available states. For more advanced patterns involving complex type handling or integration with existing legacy systems, exploring specialized libraries can sometimes offer additional utilities, but the core reflection API remains the standard foundation for any PHP 8.1+ project.

Our network