Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmag's avatar
Level 6

When to use pure enums and when backed?

When should you use backed enums instead of pure enums? The use case of a backed enum when you need a different representation than the enum case name is clear to me, like for example:

enum WeekDayEnum: int
{
    case Monday = 1;
    case Tuesday = 2;
    // etc.
}

But in what cases would you use the backed enum like this:

enum PostStatus: string
{
    case Draft = 'draft';
    case Published = 'published';
}
1 like
1 reply
LaryAI's avatar
Level 58

Backed enums in PHP, introduced in PHP 8.1, are enums that have scalar values associated with their cases. These scalar values can be integers or strings. You should consider using backed enums when you need to associate a specific, stable value with each case that is different from the name of the case itself. This is particularly useful in scenarios where the enum needs to interact with external systems or needs to be serialized in a specific format.

Here are some scenarios where you might choose to use backed enums:

  1. Database Interaction: When you need to store the enum values in a database, you might use a backed enum with string or integer values that correspond to the database representation.
enum OrderStatus: string
{
    case Pending = 'pending';
    case Processing = 'processing';
    case Shipped = 'shipped';
    case Delivered = 'delivered';
    case Cancelled = 'cancelled';
}
  1. API Communication: If you're working with an API that expects specific string or integer values, backed enums ensure that the correct value is used in requests and responses.
enum ApiResponseType: string
{
    case Success = 'success';
    case Error = 'error';
}
  1. Serialization: When you need to serialize enum values to JSON or another format, backed enums can provide a consistent representation.
enum FileType: string
{
    case Document = 'document';
    case Image = 'image';
    case Video = 'video';
}
  1. Legacy Code Compatibility: If you're refactoring code that previously used class constants or literal strings/integers, backed enums can be a drop-in replacement that provides better type safety and validation.
enum UserRole: string
{
    case Admin = 'admin';
    case Editor = 'editor';
    case Subscriber = 'subscriber';
}

On the other hand, pure enums (without backing values) are suitable when the enum cases themselves are sufficient for comparison and you don't need to serialize the enum or interact with external systems that require specific scalar values. Pure enums are simpler and can reduce the potential for errors since there's no need to manage the association between cases and their backing values.

In summary, use backed enums when you need specific scalar values associated with each case for external compatibility or serialization, and use pure enums when the case names themselves are sufficient for your use case within the application's internal logic.

1 like

Please or to participate in this conversation.