“Update before you get outdated”.
PHP 8 is here and is now supported in Drupal 9.1 and its dependencies! November 2020 saw the big release of PHP 8. We call it a big release because of the exciting new features and optimizations it comes loaded with (which we will be talking about shortly).
Drupal 8.9 and 9.0 are however marked incompatible with PHP 8. They are still compatible with PHP 7.3 and PHP 7.4 – which happens to be the last major PHP update. PHP 7.4 will stop receiving active support from the community from November 2021. And thus, updating your website to Drupal 9.1 will be a good idea now.
Drupal 10, which is scheduled to release in June 2022, will mandate compatibility with PHP 8. Read on to find out about the amazing features PHP 8 has to offer and how you can check if your Drupal version is compatible with PHP 8.
What’s new with PHP 8 (Notable Changes)
1. JIT Compiler
JIT stands for just-in-time compilation. Starting from PHP 5.5, Zend VM became part of PHP. But 8.0 introduced JIT to address some long struggling PHP performance issues. For better performance PHP was based on OPCache using OPCode. This is precompiled code given to the processor as commands. However, it is not very native to the machine language. On the other hand, JIT provides actual machine code with a mechanism to work together with OPCache. JIT does not work automatically. We need to configure this in the php.ini file.
2. The null safe operator
You must be familiar with the null coalescing operator (??) which worked as:
$value = $var1 ?? $var2
It checks for the null value of $var1 and returns it. If it is not null, it returns $var2. But it does not work with method calls. Here, the null safe operator comes into the picture.
$value = $obj->getData()?->getValue();
Here you can call the getValue() method; even if no method $obj->getData() returns null, the code will not crash. It will return null. On the other hand, using the null coalescing operator:
$value = $obj->getData()->getValue() ?? null;
..will throw an error.
3. Named argument
PHP 8 allows you to now pass named arguments to functions. It does not depend upon the argument order. Instead, you can pass the argument name.
function named_arg_example(String $arg1, $string $arg2, $string $arg3) {}
named_arg_example(
arg1: ‘arg1 value’,
arg3: ‘arg3 value’,
arg2: ‘arg2 value’,
);
4. Match expression
Match expression is like the switch statement, except that it does not require a break statement.
$value = match($check) {
0 => ‘Value is zero’,
1, 2, 3 => ‘Value is non zero and less than 4’’
}
There are many other great new features added to PHP 8 like the constructor property promotion, Attributes, Constant type errors for internal functions, Saner string to number comparison, etc. More details can be found here.
How to perform a Compatibility Check with PHP 8 on Drupal
You can use this method to check if your version of Drupal is compatible with PHP 8 or not. For that, you will need to first make sure you have the required package – phpcompatibility. For more information on this, visit here.
Next, you should already be having Drupal installed. If not, you will need to install Drupal 9 in your system. Using composer to install Drupal is the recommended way. For information about composer installation please refer this document.
STEP 1: Drupal Installation
Use this Composer command to install recommended version of Drupal
composer create-project drupal/recommended-project [my_site_name_dir]
You will need to change [my_site_name_dir] with the folder name you want to install Drupal into.
STEP 2: Installing the required Package
After installing Drupal, you will have composer.json in your Drupal root directory. Open it in text editor and add the following code:
"require-dev": {
"phpcompatibility/php-compatibility": "*"
},
If you already have require-dev section in your composer.json file, just add
"phpcompatibility/php-compatibility": "*" to the list.
Next, you need to provide location info to the PHP code sniffer by adding the following lines to composer.json
"scripts": {
"post-install-cmd": "\"vendor/bin/phpcs\" --config-set
installed_paths vendor/phpcompatibility/php-compatibility",
"post-update-cmd" : "\"vendor/bin/phpcs\" --config-set
installed_paths vendor/phpcompatibility/php-compatibility"
}
And then run:
composer update --lock
It will install phpcompatibility and other required packages.
STEP 3: Compatibility Check
Now, use this command to check PHP compatibility for the project
vendor/bin/phpcs -p [directorypath] --standard=PHPCompatibility
--runtime-set testVersion [php version] --extensions=[file extensions]
--report-full==[path/to/report-file]
You need to replace the directory path with the directory path that the test will run on. In our case it is ‘.’ because we want to run the test in current directory (All Drupal files and folder). You should also replace the [php version] with the version you want to check compatibility with - which in this case will be 8.0. Replace the [file extensions] with file extensions like php, module, inc, install, etc. The report-full gives you the flexibility to store the report log in a file. So you will need to provide the path for the log file.
So, for our case, the command will be:
vendor/bin/phpcs -p . --standard=PHPCompatibility --runtime-set
testVersion 8.0 --extensions=php,module,install,inc
--report-full==./drupal9-php8-compatibility.txt
It will take a few minutes and you will get a drupal9-php8-compatibility.txt file, where you can check the reported log.
PHP 8 has been a major change as it has brought about some useful and attractive features and optimizations. Drupal has been adopting modern technologies and libraries since Drupal 8 and is riding along the technology wave ever since. Drupal 9.1 released in December 2020 and supports PHP 8. Time to upgrade to Drupal 9 now to leverage the best of opensource!
If you haven’t already migrated to Drupal 8, now is the time. Migrate to Drupal 8 and then enjoy easy updates to Drupal 9 and the subsequent versions.