katedev30 Aug 2025 23:42

PHP 8.1 enums are a proper language feature but they have constraints. Final classes with constants work everywhere and have fewer restrictions.

When do you choose one over the other?

Replies (6)
alex_petrov30 Aug 2025 23:48

If the set of values is fixed and known at compile time, and you want type safety (function signatures accepting StatusEnum not string), use Enums. If the values are dynamic or loaded from config, use constants or a plain class.

0
nphp31 Aug 2025 00:43

Backed enums (string, int) work well for DB storage and serialization. Pure enums (no backing type) are good for state machines where the label matters, not an underlying value.

0
ivan_morozov31 Aug 2025 00:48

Enum methods are a nice feature: you can add label(), color(), or isActive() methods directly on the enum. That logic used to live in a helper class or a match expression repeated throughout the code.

PHP
<?php
enum Status: string
{
case Active = 'active';
case Inactive = 'inactive';
case Banned = 'banned';
public function label(): string
{
return match($this) {
Status::Active => 'Active',
Status::Inactive => 'Inactive',
Status::Banned => 'Banned',
};
}
public function color(): string
{
return match($this) {
Status::Active => '#38a169',
Status::Inactive => '#a0aec0',
Status::Banned => '#e53e3e',
};
}
public function isActive(): bool
{
return $this === Status::Active;
}
}
$s = Status::Active;
echo $s->label() . ' / ' . $s->color() . "\n";
echo 'Is active: ' . ($s->isActive() ? 'yes' : 'no') . "\n";
// from DB string value
$fromDb = Status::from('banned');
echo $fromDb->label() . "\n";
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0
sergey_web31 Aug 2025 00:55

Enums cannot be extended. If you need a hierarchy of related statuses or want to add a value in a child context, a final class with typed constants gives more flexibility.

0
vova31 Aug 2025 02:02

Eloquent casting to enum types is built-in since Laravel 9. The DB value is automatically converted to the enum case on read. Makes models much cleaner.

0
katedev31 Aug 2025 03:07

ORM compatibility was the main friction point early on. Doctrine needed custom types, Eloquent casts, ActiveRecord frameworks varied. Most have good enum support now.

0
Write a reply
Markdown. ```php blocks are runnable.