Enums in PHP: How to Use Enumerations with Code Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Enums in PHP: From Chaos to Order

\n\n

Have you ever written code where an order status is stored as a string 'new', 'paid', 'shipped'? And then caught bugs because of a typo like 'shippped'? Or passed getOrderByStatus('Active') to a function, but it expected 'active'? Sound familiar? Then this article is for you.

\n\n

For a long time, PHP did not have native enumerations (enums). Developers improvised: they used class constants, arrays, even placeholder comments. But with the release of PHP 8.1, everything changed. Now we have full-fledged PHP enumerations — a powerful tool that makes code cleaner, safer, and more understandable.

\n\n

In this guide, we'll cover PHP enums from A to Z: from basic syntax to advanced techniques. You'll learn how to create enumerations, work with them, compare them, and use them in real projects. Let's go!

\n\n

What is an Enum in PHP?

\n\n

An Enum (enumeration) is a data type that is limited to a predefined set of values. Simply put: you define a list of possible options, and a variable can only take one of them. No random strings, no unchecked values.

\n\n

In PHP 8.1, enumerations are implemented as special classes. Each value is an object, not just a constant. This provides a host of advantages:

\n\n
    \n
  • Strict typing — the compiler won't allow 'pending' instead of Status::Pending
  • \n
  • Safety — no accidental typos
  • \n
  • Readability — the code speaks for itself
  • \n
  • IDE support — autocompletion works great
  • \n
\n\n

Enum Syntax in PHP

\n\n

Creating an enumeration in PHP is simple. Use the enum keyword:

\n\n
<?php\n\nenum OrderStatus\n{\n    case New;\n    case Pending;\n    case Processing;\n    case Shipped;\n    case Delivered;\n    case Cancelled;\n}\n
\n\n

Now we can use OrderStatus as a type:

\n\n
<?php\n\nfunction updateOrderStatus(OrderStatus $status): void\n{\n    // Handle the status\n}\n\n// Pass a value:\nupdateOrderStatus(OrderStatus::Shipped);\n
\n\n

Try passing the string 'shipped' — PHP will throw a type error. That's the magic of enum in PHP.

\n\n

Backed Enums

\n\n

Often we need to associate each case with a specific value — for example, a number or string for the database. In PHP, this is called a backed enum:

\n\n
<?php\n\nenum OrderStatus: string\n{\n    case New = 'new';\n    case Pending = 'pending';\n    case Processing = 'processing';\n    case Shipped = 'shipped';\n    case Delivered = 'delivered';\n    case Cancelled = 'cancelled';\n}\n
\n\n

Now each case has a string value. You can access it via the value property:

\n\n
<?php\n\n$status = OrderStatus::Shipped;\necho $status->value; // 'shipped'\n
\n\n

You can also create a case from a value in reverse:

\n\n
<?php\n\n$status = OrderStatus::from('shipped');\necho $status->name; // 'Shipped'\n
\n\n

The from() method will throw an exception if the value is not found. If you want to get null instead, use tryFrom().

\n\n

Methods in Enum: Behavior Inside Enumerations

\n\n

One of the cool features of PHP enum is the ability to add methods directly inside the enumeration. This turns an enum from a simple list of constants into a full-fledged object with logic.

\n\n
<?php\n\nenum OrderStatus: string\n{\n    case New = 'new';\n    case Pending = 'pending';\n    case Processing = 'processing';\n    case Shipped = 'shipped';\n    case Delivered = 'delivered';\n    case Cancelled = 'cancelled';\n\n    public function label(): string                

Blogs

Book Recommendations