DEV Community


First Look at PHP 7.4 for WordPress Developers

bolex88 profile image James Titus Wilson ・3 min read

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.

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.

Discussion (0)

Editor guide