PHP Classes

How to Improve Your PHP Code Using Enumerations Introduced in PHP 8.1 or Later in 6 Steps with Code Examples

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Improve Your P...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  


Viewers: 300

Last month viewers: 1

Categories: PHP Tutorials, News

Support for enumerations is a recent new PHP feature requested by many developers that recognize the benefits of this new feature.

PHP 8.1 introduced the support for enumerations as a data type built-in in the PHP language.

If you use constants to emulate the benefits of enumerations in versions older than PHP 8.1, you will appreciate this new feature.

Still, you need to be careful when upgrading your PHP code to a newer version if you have not yet used PHP 8.1 or later.

Read this article to learn more about enumerations in PHP and how to upgrade smoothly to a new PHP version to benefit from this feature minimizing the risks of doing an upgrade and avoiding breaking the code of your PHP applications that used to run on PHP versions older than 8.1.

Loaded Article

In this article you will learn:

What Are Enumerations in PHP

Why Enumerations Are Better Than Using Constants with Code Examples

How Can You Evolve Your PHP Code From Using Constants and Start Using Enumerations in 6 Steps

What Are Enumerations in PHP

Enumerations are a type of value introduced in PHP 8.1 as a native value type according to this RFC (Request For Change) Document.

An enumeration can have different values described as cases and associated with names. Here is an example of an enumeration type:

enum TrafficLight: string {
  case Red = 'R';
  case Green = 'G';
  case Blue = 'B';

$light = TrafficLight::Green;

Why Enumerations Are Better Than Using Constants with Code Examples

Before PHP 8.1, you could use constants to define values of a certain that would not change during the execution of a PHP script. Here is an example of how it would look like:

define('TrafficLightRed', 'R');
define('TrafficLightGreen', 'G');
define('TrafficLightBlue', 'B');

$light = TrafficLightGreen;

Constants also would solve the problem of assigning a value to a variable in a way that you have readable code that you can look at and understand what it does.

However, constants do not make it possible to assign a specific type to a variable. For instance, in the example above, the variable $light would be set to a string value.

If by mistake, you assign a value like TrafficLightGren , before version 8.0, PHP would still set the value with a warning that looks like this:

Warning: Use of undefined constant test - assumed 'TrafficLightGren' (this will throw an Error in a future version of PHP) in test.php on line X

So instead of assigning the string 'G', it would assign 'TrafficLightGren'. That may cause all sorts of bugs in your applications, like storing wrong information in a database table field that is only expected to have valid values.

You can use the PHP error log and detect mistakes like this if you watch your error log before PHP 8.0.

If you use enumerations and make a mistake typing TrafficLight::Gren instead of TrafficLight::Green, PHP will issue a fatal error like this:

Fatal error: Uncaught Error: Undefined constant TrafficLight::Gren in test.php:X
Stack trace:
#0 {main}
  thrown in test.php on line X

This way, you can detect mistakes like this earlier in your PHP development environment. That will help you to prevent shipping code with errors to the production.

How Can You Evolve Your PHP Code From Using Constants and Start Using Enumerations in 6 Steps

Here are the steps that you should follow to make a smooth transition to using code that uses enumerations instead of constants:

1. Upgrade PHP in your development environment to PHP 8.1 or later

First, you need to understand that to use enumerations, you need to upgrade your application to run in PHP 8.1 or later.

There were several backward incompatible changes in different PHP new versions. Your code may stop working in other places unrelated to using constants and enumerations.

2. Make sure you have enabled your PHP the PHP error log to catch errors that need to be fixed

The PHP error log is usually a file that you can configure PHP to output errors that you have in your code.

It shows the name of the PHP script files and lines that cause the errors. This will help you to locate the code that needs to be fixed.

You may also want to read this article about doing fast bug fixing using a PHP error log package called Log watcher.

3. Test all your application code in your development environment

Before changing your code to use enumerations, check if all your code works well in the newer PHP. If some code stops working due to changes in the PHP language implementation, you need to fix your code to make it work well.

Ideally, you should use a set of test scripts to test your application to make the tests work faster.

If you do not use your test scripts, adding more tests would be a good idea to ensure that your code works well now and in future upgrades to newer PHP versions.

Still, your test scripts may likely not cover all the use cases your application supports. So it is always good to test your application manually by accessing it like a regular user. Then check the PHP error log file to check if you see warnings or fatal errors caused by code that needs to be fixed.

Personally I use this PHP package to test features of the PHP language and of my applications. You may also use other popular PHP testing packages like PHPUnit or other packages.

4. Change your code to use enumerations instead of constants

First, consider making these changes on new code rather than changing all old code to use enumerations.

That will be safer than changing a lot of code simultaneously.

Still, if you want to use enumerations immediately, change your code to one place that uses constants to start using counts.

If possible, try to add tests to the code you change to make sure that code will not break due to your changes.

Then you can replace the code that uses constants to use enumerations, adding the code that defines the total and removing the code that defines constants.

Then replace the code that uses constant values to use enumeration case values.

5. Use a testing environment to test your application in an environment closer to the production environment

Often the development environment is different from the production environment.

So it is good to put the code to work in a testing environment that has a similar configuration to the production environment.

The production environment often runs in the same hosting machine or network with a slightly different configuration.

6. Upgrade the production environment to the newer PHP version

After you passed the tests in your production environment, it safer to upgrade the production environment.

Still you may find issues in the production environment due to situations that escaped your tests.

So be ready to fix the remaining issues in the code that you run in the production environment very quickly.

I advise that you do the production environment upgrades on a weeking or on a low traffic day to minimize the impact of application issues in your application.

You need to be a registered user or login to post a comment

Login Immediately with your account on:


No comments were submitted yet.

  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Improve Your P...   Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)