PHP 7.4 released

Photo by Ryan Quintal on Unsplash

The PHP project announced the release of PHP 7.4 on November 28th, 2019. As usual, they have also published a migration guide to assist developers in updating their code to support PHP 7.4. This release continues to bring smaller gradual improvements to PHP just like 7.3 and 7.2 did, and it does not include drastic changes as 7.1 and 7.0 did.

Security improvements and speed optimization work have continued: PHP 7.4 is reported to be once again slightly faster than any previous release. Those alone are good reasons to consider upgrading to PHP 7.4. In addition, there are some new features.

Typed Properties

In PHP 7.4 it is now possible to add extra validation to properties, e.g. variables in classes, to enforce that any code that calls them uses them properly. The notation is simple, just add the type in front of the property definition:

class User {
    public int $id;
    public string $name;
}

A null coalescing assignment operator

While PHP 7.0 introduced the ?? operator, this release now also has the ??= operator. This is handy to check that the value exists and then assign it in one line, instead of having to do separate isset() checks (or having PHP Notices emitted). Example:

$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
    $array['key'] = computeDefault();
}

Unpacking inside arrays

A new notation ... can be used to unpack an array. Consider the code example below:

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', $parts, 'watermelon'];
// ['banana', 'orange', ['apple', 'pear'], 'watermelon'];

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];

Numeric literal separator

Reading in the source code a value like 10000000 can be hard. Can you immediately spot if it says a million or 10 million or what? In PHP 7.4 you can have underscores (e.g. 10_000_000) in the value for readability without affecting how PHP uses the value.

More information about the changes in PHP 7.4 are available on the PHP website. Developers should also note the backward-incompatible changes and deprecated features.

WordPress 5.3 supports PHP 7.4

The WordPress core has official support for PHP 7.4 since version 5.3. It will, however, take some time before all plugins update to support PHP 7.4. There are also some PHP modules that are not yet available for PHP 7.4 (e.g. Tideways). Thus we do not recommend upgrading to PHP 7.4 just yet on production sites, but it would make sense to start doing it in early 2020.

PHP 7.4 Available at Seravo

Just like after previous PHP releases, Seravo has been very quick in making it available for our customers. The time frame is usually a couple of weeks after the official upstream release. We are still waiting for some PHP modules to become officially available for PHP 7.4 and will then start rolling it out into production, shadows and the development environments we provide (Vagrant and Docker).

At the same time, we will also introduce automatic PHP compatibility scanning for our customers’ code base and make sure all the tooling we provide will make it as easy as possible for developers to test and upgrade to PHP 7.4.

To be notified immediately when the tooling is available, we recommend subscribing to our developer newsletter.