Retrieving Enum Values from String Names in PHP
PHP enums, introduced in version 8.1, provide a robust way to define a set of named constants. While the built-in from() method allows you to resolve an enum instance based on its integer backing value, the reverse operationâretrieving an enum instance given just its string nameâis not natively supported by the language core in the same direct way.
Consider the scenario where your application receives a status identifier as a string from a form submission or an API payload, for example: "REVIEWED". You cannot simply cast this to Status::from("REVIEWED"). Attempting to pass a string directly to from() will throw a ValueError because the method expects either an integer that matches the backing value or an existing enum instance.
To resolve a name stored in a string back into an enum instance, you must leverage the internal reflection capabilities of PHP. Enums in PHP expose their cases as a static property $cases, which is an array containing all defined case instances. By iterating over this collection or using array_search(), you can map the string name to the corresponding enum object.
Here is how you can implement this pattern:
enum Status: int
{
case ACTIVE = 1;
case REVIEWED = 2;
}
function getStatusFromName(string $name): ?Status
{
return Status::cases()
->first(fn ($case) => $case->name === $name);
}
// Usage
$statusString = 'REVIEWED';
$retrievedStatus = getStatusFromName($statusString);
if ($retrievedStatus instanceof Status) {
echo $retrievedStatus; // Outputs: 2
}
In this approach, we iterate through the $cases array using a closure that compares the case's .name property against the input string. This returns the enum instance if found, or null if no matching case exists. You must handle the null return value carefully in your application logic to avoid undefined behavior when processing user input or external data.
An alternative implementation uses array_search() on a custom array mapping, though this requires manually constructing that map first. However, accessing $cases directly is the most idiomatic and maintainable solution because it relies on the enum's own internal structure rather than maintaining parallel arrays.
It is important to note that case names are case-sensitive. If your input string is "reviewed" (lowercase) but the enum case is defined as REVIEWED, the lookup will fail. You may need to apply a normalization step like strtoupper() to the incoming string before performing the check, depending on how your application ingests data.
If you are working within a Laravel project, this pattern integrates seamlessly with type-safe form request validation or API response handling. While there is no dedicated helper in the core framework for this specific lookup, leveraging PHP's native reflection and array utilities keeps dependencies low and logic clear. For teams managing large codebases where string-to-enum mapping becomes frequent, consider creating a utility class that encapsulates this logic to ensure consistency across the application.
Ultimately, while PHP enums offer powerful type safety, they require explicit handling for name-based lookups. Understanding how $cases works allows you to bridge the gap between dynamic string data and static type definitions without sacrificing performance or clarity.
