Native enums are coming to PHP

The next bigger version of PHP is going to be released in November 2021. Version number 8.1 will come with few interesting changes, but for the most of the developers, the most significant one will be implementations of enumerated types.

What enums are? Enumeration types are basically yet another data type, which contains a list of predefined values. It's a very convenient solution, when you want to force the developer to pass only a specified range of cases. Here is a quick example. For some reason (god only knows why), You are dealing with seasons. Instead of working with strings like "Spring", "Summer", "Autumn" and "Winter", you can create Season enum:

enum Season{
  case Spring;
  case Summer;
  case Autumn;
  case Winter;
}

New you can request to pass in your function the Season enum type:

function(Season $season){
  if($season === Season::Summer){
    echo 'It summer! It must be hot!';
  }
}

Backed enums

You can also create enum with assigned scalar value for every option. It is helpful, when you want to, e.g. pass your data to a database. It's called backed enums. Here is and example:

enum DayOfWeek: int {
  case Monday = 1;
  case Tuesday = 2;
  case Wednesday = 3;
  case Thursday = 4;
  case Friday = 5;
  case Saturday = 6;
  case Sunday = 7
}

Calling specific enum options will return something resembling an object. Object with two properties – name, and value. So, you can access enum data in similar way to objects parameters.

DayOfWeek::Friday->value // It will return 5

Ok, we know how to get value of enum options. What about the reverse direction? How to get enum name, knowing just it's value? PHP introduces two "static" method, which can be call on the enum – ::from() and ::tryFrom().

DayOfWeek::from(5)

Will result with:

DayOfWeek Enum:int
(
    [name] => Thursday
    [value] => 4
)

Remember that backed enums can contain cases only of one type – for example integer like in the example above. It's not possible to mix types, neither mix cases with values and pure cases (it's term for case without assigned value). So, there are only two types of enums in PHP – pure and backed ones.

Enumerated Methods

The above features are present in most of the programming languages which support enums. But PHP will go one step further. It provides Enumerated Methods Enums will work in some way like classes. You will be able to add your method, implement interfaces, even use traits. Look at this:

enum MatchStatus {
  case NotStarted;
  case FirstHalf;
  case SecondHalf;
  case FinishedRegularTime;
  case FinishedAfterPenalties;

  public function type(): string {
    return match($this) {
      Status::NotStarted => 'Not started',
      Status::FirstHalf, Status::SecondHalf => 'Ongoing',
      Status::FinishedRegularTime, Status::FinishedAfterPenalties => 'Finished',
    };
  }
}

We've created enum MatchStatus which contains some soccer event statuses. Then we implement type() method, which will return status type as string value. This is how we can use it:

$matchStatus = MatchStatus::SecondHalf;
$matchStatus->type();

The result of calling type method will be 'Ongoing' string. If you haven't used PHP8 yet, You can be not familiar with match expression. Read about it here.

More information and examples of use you can find in RFC of enums.

What's your opinion about PHP enumerated types? What do you think about syntax, and rich enum features, which are not usually presented in other languages? Was it worth to wait? Share your thoughts!

23