Although the concept of enums is not novel in programming, PHP introduced this feature relatively recently, with its 8.1 version. Now, let’s explore the possibilities it offers.

Enum in PHP is declared in this way:

enum OrderStatus {
    case PENDING;
    case CONFIRMED;
    case SHIPPED;
    case DELIVERED;
    case CANCELLED;
}

So it represents a collection of constants. We can use it in different ways. For example, suppose we have an Order class. We can define a typed parameter in constructor like this:

class Order
{
    public function __construct(
        public OrderStatus $status, 
    ) {}
}

// Then later create an object like this:

$order = new Order(OrderStatus::PENDING);

This serves as a fundamental illustration. Similar to a regular class, you have the capability to define methods within an enum. This can be beneficial in specific scenarios. For instance, envision an order list page where we display the status of each order using Bootstrap badges, each having a specific CSS class for different status types. Achieving this is straightforward by creating a method within the enum, as demonstrated here:

enum OrderStatus{
    case PENDING;
    case CONFIRMED;
    case CANCELLED;
    
    //defining a method
    
    public function getLabel(){
        return match($this){
            self::PENDING => "<span class='badge bg-secondary'>Pending</span>",
            self::CONFIRMED => "<span class='badge bg-success'>Confirmed</span>",
            self::CANCELLED => "<span class='badge bg-danger'>Cancelled</span>"
        };
    }
}

//Later we can use it like this:

$status = OrderStatus::CONFIRMED;
echo $status->getLabel();

Another valuable feature is the ‘Backed Enum.’ This feature enables us to assign values to enum constants. Furthermore, it permits type declarations within the enum definition. Presently, it supports string and integer types only. To illustrate, we can define our OrderStatus enum as follows: 

enum OrderStatus: int {
    case PENDING = 1;
    case CONFIRMED = 2;
    case CANCELLED = 3;
}

Rather than employing a constant, we can now utilize the value. This approach proves especially valuable when dealing with database values. For instance, we may store an integer value in the ‘status’ column of the ‘order’ table. When the enum is required at a later time, we can use it as demonstrated below: 

$status = OrderStatus::from(2); //equivalent to OrderStatus::CONFIRMED

//Or we can use tryFrom

$status = OrderStatus::tryFrom(10); //it returns null incase value is unknown

//Conversely, we have the option to retrieve the value from a constant and store it in the database

$status = OrderStatus::PENDING->value; //returns 1

Similar to a standard PHP class, it’s also feasible to implement an interface in an enum.

In conclusion, enums can be an extremely beneficial feature. While it’s true that achieving similar outcomes is possible without using enums, the question arises: don’t you aspire to structure our code in a more organized and cleaner manner? If the answer is affirmative and you work as a full-time PHP developer, it’s advantageous to acquaint yourself with all the native functions and language constructs that PHP provides.