Unlocking the Power of PHP Enums: Best Practices for Effective Use

php enum

PHP added Enums with version 8.1. Finally! Many other programming languages such as Java and C# have enums for a long long time. And as a little time has passed since enums were added to PHP core, I quickly want to give an overview and continue with the some personal thoughts about when and how to use.

PHP Enums

Enums are declared very similar to classes:

They can be used as type hinted properties or return types for methods and constructor values. The value of the constants are normally managed by PHP, but however, the “backed” enums can have values and get created out of them.

Why Enums instead of Constants or Primitive Values?

You could ask: why do we need enums if there are (class) constants? Or why do we not just define the numbers 1, 2, 3, 4 as HEART, SPADE, CLUB, DIAMOND?

First of all, there is readability. Look at the numbers 1, 2, 3, 4. Their meaning is up to your interpretation, don’t they? Further, if you start working like this, it is likely that you will follow this pattern for other use cases as well. For instance, besides of the suits, statuses might be 1, 2, 3, 4 as well and might mean DRAFT, REVIEWED, SCHEDULED, PUBLISHED. In this case, you would introduce ambiguity.

Notice that this ambiguity is not finally solved when you move the values to a (class) constant. Sure, you would keep a little order by using constants, but at a very low level you still deal with primitive values and there is technically no obstacle to use a hardcoded value instead of the constant. Using real enums is not only better in terms of readability and error prevention, enums are also better in terms of type safety and maintenance.

When to not use Enums?

There are also situations where you should not use enums. A very good measure for using enums or not is the value set: do the values in the set change often? can you add new values infinitely? do you need type safety (especially for programming languages like PHP)? is performance/memory a critical aspect of your application (e.g. real time applications)?

If you can answer “yes” to the questions above, you should probably avoid enums. Enums are useful when you have a limited set of values that do not change often and increase type safety of your code base.

Enums increase Code Quality

In my past projects, I have had very good experience with strict and type safe code bases. Personally, I classify enums under “type safety” and a good tool to increase strictness of source code. There are no more value checks, type castings, null checks and so on – you simply define an enum parameter in your method and the programming language is doing all the work.

Enums in programming languages are also a good extension for database enums. It is possible to represent the enum database fields in the programming language without having complex converter logic. Therefore, I personally try using enums wherever possible and wherever the conditions above are given.