First Look at PHP 7.4 for WordPress Developers

PHP 7.4 is slated for release on November 28, 2019. WordPress 5.3 will also include several commits that address deprecated features.

It’s unlikely that most WordPress plugin and theme developers will be able to use the new PHP 7.4 features for a while except when working on setups where they have some measure of control over the environment. Given WordPress’ reluctance to bump the minimum PHP version to the latest supported versions, it could be years before it’s safe to use new features. However, developers should make sure their plugins and themes are compatible with 7.4. It’s also good practice to keep up with current features.

WordPress 5.2 bumped the minimum requirement to PHP 5.6 with a recommendation of PHP 7.3. The following is the breakdown for current PHP usage stats for WordPress installs:

  • PHP 7.3 – 9.6%
  • PHP 7.2 – 22.2%
  • PHP 7.1 – 13.5%
  • PHP 7.0 – 14.9%
  • PHP 5.6 – 25.7%

Currently, PHP 7.2 and 7.3 are the only versions receiving active support. PHP will drop security support for version 7.1 on December 1.

New Features

PHP 7.4 includes a number of new features that will make writing code easier. The following is a small overview of some of the features that will likely be useful for WordPress plugin and theme authors.

Typed Properties

PHP first introduced type hinting in 5.0 and has continued improving the feature. PHP 7.0 introduced return type declarations. PHP 7.4 steps it up a notch and allows developers to declare types for class properties. This lets developers make sure the type of data assigned to a specific property is always that data type.

In the following example, the $id property can only be assigned an integer and the $title property a string. Attempting to assign the wrong data type will result in a fatal error if strict types are declared. Otherwise, PHP will coerce the value to the correct type.

class Event {

    public int $id;

    public string $title;
}

Arrow Functions

Many developers have been clamoring for arrow functions similar to JavaScript. They are pretty sweet to use. However, PHP’s arrow functions, also called “short closures,” are slightly different. PHP 7.4 introduces the fn keyword (now a reserved keyword). Short closures can only contain one expression and have no return keyword. They’re not quite as powerful as their JavaScript cousins, but they do offer a quick solution for some scenarios.

For example, the following code snippet shows two different ways to build an array of user emails from a collection of user objects.

// Old way, but still acceptable.
$value = array_map( function( $user ) {
    return $user->user_email;
}, $users );

// New way.
$value = array_map( fn( $user ) => $user->user_email, $users );

Unpacking Inside of Arrays

Argument unpacking was introduced in PHP 5.6, so this may still be unfamiliar territory for many developers in the WordPress space. It’s been an invaluable feature in some of my projects.

PHP 7.4 allows developers to unpack an array within an array. This should be faster than array_merge() because the spread operator (...) is a language construct rather than a function.

Consider the following example of injecting an array of two colors within a larger array of colors.

$colors_a = [ 'green', 'blue' ];

$colors_b = [ 'red', ...$colors_a, 'yellow', 'purple' ];

// [ 'red', 'green', 'blue', 'yellow', 'purple' ];

Null Coalescing Assignment Operator

The null coalescing assignment operator is a shorthand way of combining an isset() check with a ternary operator.

The following example shows how to check the $product['id'] variable. If it’s set, do nothing. Else, assign it the value on the right. You can see the evolution of the code between PHP versions. It’s much simpler to write in 7.4.

// Pre-PHP 7.0.
$product['id'] = isset( $product['id'] ) ? $product['id'] : 0;

// PHP 7.0.
$product['id'] = $product['id'] ?? 0;

// PHP 7.4.
$product['id'] ??= 0;

Deprecated Features

PHP 7.4 will deprecate several features. Plugin and theme authors will want to consult the complete list to determine whether they should update any outdated code. I cherry-picked a few items that I’ve seen in plugin and theme code over the last few years. Most other deprecated features are unlikely in current code bases.

Nested Ternary Operators Without Parenthesis

Nested ternary operators are generally something you should avoid as a developer. Seriously. Just don’t write them. They’re tough to follow and are prone to bugs due to human error. However, if you’re going to throw caution to the wind and dismiss good advice, at least use parenthesis. PHP 7.4 deprecated nested ternaries without them.

// Not OK (deprecated).
$value = $a ? $b : $c ? $d : $e;

// OK.
$value = ( $a ? $b : $c ) ? $d : $e;

// OK.
$value = $a ? $b : ( $c ? $d : $e );

Using array_key_exists() on Objects

Developers should not use array_key_exists() on objects simply because objects are not arrays. Instead, use the more appropriate property_exists() function or isset().

// Not OK (deprecated).
if ( array_key_exists( $property, $object ) ) {}

// OK.
if ( property_exists( $object, $property ) ) {}

// OK.
if ( isset( $object->$property ) ) {}

Array and String Offset Access With Curly Braces

Using curly braces for array and string offset access is a bit more of a rarity, but I’ve seen it in the wild a couple of times. Make sure you’re using square brackets such as [] instead of curly brackets like {}.

// Not OK (deprecated).
$value = $a{ $key };

// OK.
$value = $a[ $key ];
Would you like to write for WP Tavern? We are always accepting guest posts from the community and are looking for new contributors. Get in touch with us and let's discuss your ideas.

23 Comments


  1. I am probably unreasonably excited for typed properties. I’m just hoping I can jump to 7.4 ASAP both for professional and hobby projects (the latter being more likely).

    Report


  2. Finally, I can now answer my .NET developer colleagues who always taunt me for not having types properties lol

    Report


  3. I made a plugin fully compatible with PHP 7.3, but the clients immediately wanted to kill me because their hosting still has them with PHP 5.6 and they didn’t want to update the versions. It is very complicated to make a plugin with the PHP 7.4 version when the plugin is already on the market. But if the plugin is new you can send a warning that is plugin only works with PHP7.3 or higher.

    Report


    1. those sound like some very dangerous clients

      Report


  4. Great roundup, I didnt even know about the null coalesing in php 7, been writing a lot of unecessary code dangit.

    How will some of these features work when running an older version of php, will they just blow up (cause errors)?

    Report


    1. You’d have to throw in some compatibility checks to support older versions of PHP. Otherwise, you’d get errors on the older install.

      Report


  5. Null coalescing assignment operator is the thing I like most. Writing ?? is much better than isset(). Sadly it’s not available for PHP < 7.

    Report


  6. Hello,

    I am not a developer, so I want to know that can I upgrade my website with PHP 7.4

    I don’t want any issue with my website

    Thanks

    Report


    1. PHP 7.4 isn’t available to the general public yet. It’ll also need to be made available by your Web host. Depending on who that is or what your hosting environment looks like, it could be months or longer before you can upgrade your PHP version.

      To get ahead, you might want to check out the PHP Compatibility Checker plugin, which will let you know what versions of PHP your themes and plugins are compatible with. Please make sure to read its notes and disclaimers about how the plugin works.

      Report


    2. not a good idea. besides this is your host’s job so don’t worry about it.

      Report


  7. Attempting to assign the wrong data type will result in a fatal error.

    …only if `strict_types` are declared, otherwise the PHP will coerce the value as best it can.

    Report


    1. Ahhh, “if strict types are declared”. I didn’t click to what that meant when reading the article.

      Can we even declare strict types with WordPress though? Last I checked, it threw a bazillion errors when doing that.

      Report


    2. otherwise the PHP will coerce the value as best it can.

      To be more specific: even then, PHP will only attempt to coerce/juggle scalar values.

      See: https://3v4l.org/0e6Co

      Report


  8. Thanks for the rundown on the latest and greatest PHP changes :)

    I wish people wouldn’t use ternary operators even for single levels. I find them terribly confusing and always have to look at them a few times to work out what it’s actually doing. I shudder to think of how confusing a nested one will be :/

    Report


    1. I love them. It depends on what you are used to and your code editor, i guess.

      Very useful to ensure edge cases end up with a valid type of variable. No need for a multi-statemnet if block just for that. I often find those confusing. Full if blocks is best for the main logic.

      Report

Comments are closed.