Enums in PHP: From Chaos to Order
\n\nHave 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.
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\nIn 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\nWhat is an Enum in PHP?
\n\nAn 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\nIn 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 ofStatus::Pending\n - Safety — no accidental typos \n
- Readability — the code speaks for itself \n
- IDE support — autocompletion works great \n
Enum Syntax in PHP
\n\nCreating an enumeration in PHP is simple. Use the enum keyword:
<?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\nNow we can use OrderStatus as a type:
<?php\n\nfunction updateOrderStatus(OrderStatus $status): void\n{\n // Handle the status\n}\n\n// Pass a value:\nupdateOrderStatus(OrderStatus::Shipped);\n\n\nTry passing the string 'shipped' — PHP will throw a type error. That's the magic of enum in PHP.
Backed Enums
\n\nOften 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\nNow each case has a string value. You can access it via the value property:
<?php\n\n$status = OrderStatus::Shipped;\necho $status->value; // 'shipped'\n\n\nYou 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\nThe from() method will throw an exception if the value is not found. If you want to get null instead, use tryFrom().
Methods in Enum: Behavior Inside Enumerations
\n\nOne 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