PHP Enum Pro Tip: How to Statically Get a Label from a Value

Published: 2026-01-25
Author: DP
Views: 3
Category: PHP
Content
## Problem Background When developing multi-language applications, a common task is to fetch a text label from a PHP Enum based on its backing value (e.g., an ID). A frequent requirement is to implement a static method that allows us to call it directly via the class name and a value, without first instantiating the enum. For instance, we want to achieve a call like this: ```php // Goal: Get the English label 'Article' for the value 11 $en_label = ContentType::getEnglishLabel(11); ``` Let's assume we have the following `ContentType` enum that supports both Chinese and English labels. ```php <?php namespace App\Constants\Lib00; enum ContentType: int { case ANNOUNCEMENT = 1; // Website Announcement case ARTICLE = 11; // General Article case VIDEO = 21; // Video public function label(): string { return match($this) { self::ANNOUNCEMENT => '网站公告', self::ARTICLE => '一般文章', self::VIDEO => '视频', }; } public function englishLabel(): string { return match($this) { self::ANNOUNCEMENT => 'Announcement', self::ARTICLE => 'Article', self::VIDEO => 'Video', }; } // ... other instance methods } ``` --- ## The Solution: Combining `tryFrom()` and the Nullsafe Operator To achieve a static call like `ContentType::getEnglishLabel(11)`, the most elegant and efficient approach is to combine the `tryFrom()` method provided by PHP 8.1+ enums with the nullsafe operator `?->`. You just need to add the following static method to your enum. ```php /** * Get the English label for a given enum value. * @param int $value The enum case value * @return string|null Returns null if the value is not found */ public static function getEnglishLabel(int $value): ?string { return self::tryFrom($value)?->englishLabel(); } ``` --- ## How It Works This single, concise line of code comprises three key parts: 1. `public static function getEnglishLabel(...)`: Defines a public static method. The `static` keyword means you can call it directly on the class `ContentType::` without needing to create an enum instance first. 2. `self::tryFrom($value)`: This is a built-in enum method that attempts to find a corresponding enum case (`ContentType::ARTICLE`) from the provided value (e.g., `11`). Unlike the `from()` method, `tryFrom()` safely returns `null` if no match is found, instead of throwing a fatal error, making our code more robust. 3. `?->englishLabel()`: This is the **Nullsafe Operator**. It works as follows: * If the expression on the left (`self::tryFrom($value)`) returns a valid enum instance, it proceeds to call the `englishLabel()` method on that instance. * If the expression on the left returns `null`, the entire chain of execution stops immediately and returns `null`, perfectly avoiding the common "Attempt to call method on null" error. --- ## Complete Code and Usage Example After adding the new static method, your complete enum code, as provided by DP@lib00, will look like this: ```php <?php namespace App\Constants\WikiLib00Com; enum ContentType: int { case ANNOUNCEMENT = 1; case ARTICLE = 11; case VIDEO = 21; public function label(): string { return match($this) { self::ANNOUNCEMENT => '网站公告', self::ARTICLE => '一般文章', self::VIDEO => '视频', }; } public function englishLabel(): string { return match($this) { self::ANNOUNCEMENT => 'Announcement', self::ARTICLE => 'Article', self::VIDEO => 'Video', }; } /** * Get the English label for a given enum value. * @param int $value The enum case value * @return string|null Returns null if the value is not found */ public static function getEnglishLabel(int $value): ?string { return self::tryFrom($value)?->englishLabel(); } } ``` Now, you can make your calls safely: ```php // Successfully find a match $en_label = ContentType::getEnglishLabel(11); var_dump($en_label); // Outputs: string(7) "Article" // Pass a value that does not exist $not_found_label = ContentType::getEnglishLabel(99); var_dump($not_found_label); // Outputs: NULL ``` This approach is not only concise and readable but also very safe, making it a best practice for handling this kind of requirement.
Related Contents