If your code still has const STATUS_DRAFT = 'draft'; scattered across three classes and a database column that occasionally contains 'Publsihed', enums are the fix you've been putting off.
PHP has had enums since 8.1. The bit that trips people up is turning a plain string back into an enum. There are two methods for that, from() and tryFrom(), and picking the wrong one either crashes your page or quietly hides a bug.
A backed enum in one minute
A backed enum is an enum where every case carries a fixed string or int value. That value is what you store in the database or send in JSON.
```php <?php declare(strict_types=1);
enum Status: string { case Draft = 'draft'; case Published = 'published'; case Archived = 'archived';
public function label(): string { return match ($this) { Status::Draft => 'Draft', Status::Published => 'Live', Status::Archived => 'Archived', }; } } ```
Enums can have methods, so the label lives next to the value instead of in a helper file nobody can find. The match has to cover every case, or PHP throws an UnhandledMatchError when it hits a missing one.
from(): when a bad value is a bug
Status::from('published') gives you Status::Published. Give it a value that isn't a case and it throws a ValueError.
```php $status = Status::from('published'); echo $status->label(); // Live
try { Status::from('deleted'); } catch (ValueError $e) { echo get_class($e); // ValueError } ```
Use from() for data you control, such as a value you wrote to your own database. If that column holds something that isn't a case, something has gone wrong and you want to hear about it loudly.
tryFrom(): when a bad value is normal
tryFrom() returns null instead of throwing. That's what you want for anything a user can type or tamper with: form fields, query strings, API input.
```php $input = $_GET['status'] ?? ''; $status = Status::tryFrom($input) ?? Status::Draft; ```
The null coalescing operator gives you a sensible default in one line. If a missing value should be an error rather than a default, check for null and return a 400 or a validation message instead.
cases(): free dropdowns
Status::cases() returns every case in the order you declared them, which is handy for building a select list.
```php foreach (Status::cases() as $case) { echo $case->value, '=', $case->label(), PHP_EOL; } ```
That prints draft=Draft, published=Live and archived=Archived. All the examples in this post were run on PHP 8.5.
The rule of thumb
- Your own data:
from(). Fail loudly. - Someone else's data:
tryFrom(), then a default or a validation error. - Never compare enum values as strings around your codebase. Convert once at the edge, then pass the enum around.
In other words, the enum is the bouncer. Let it check IDs at the door, then stop worrying about who's inside.
Want more like this?
Clean, typed PHP like this is what my book PHP Done The Correct Way is about, with a free companion code page. If you'd like help tidying up a PHP project, have a look at PHP AIR, and for more tips see the PHP AIR blog.