A look at some of PHP 7.4’s new features

← Back
A laptop with PHP code on the screen

PHP 7.4 was released on 28 November 2019 and with it comes a lot of new features. In this post we’ll examine a few of the more interesting ones along with examples of how they can be used. PHP has long been the punchline in low-effort memes, and though some of the disdain for the language is justified, more recent versions have made significant improvements to PHP and this latest one is no exception.

Typed properties

With the introduction of typed properties, class properties now support type declarations. This means that only values of the specified type can be assigned to those properties.

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

Arrow functions

Arrow functions will be familiar to anyone who has been writing ES6 and beyond JavaScript since 2015. They offer a nice shorthand for writing anonymous functions with implicit returns, and use implicit by-value scope binding, which means that it is not possible to modify any values from the outer scope. In other languages such as JavaScript, the ($x) => $x * $y syntax is used but in PHP it must be preceded by the fn keyword. Unfortunately, PHP already uses the => syntax for specifying keys and values in array declarations, so the fn keyword is necessary for backwards compatibility.

$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
// $nums = array(10, 20, 30, 40);

Null coalescing assignment operator

Null coalescing was introduced in PHP 7.0 back in 2015. For those who aren’t familiar with null coalescing, it is essentially a shorthand for combining an isset and a ternary expression to set a value.

// Fetches the value of $_GET['user'] and calls computeDefault()
// if it does not exist.
$username = $_GET['user'] ?? computeDefault();
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : computeDefault();

The new null coalescing assignment operator allows us to simplify that further by assigning at the same time:

$array['key'] ??= computeDefault();

Unpacking inside arrays

As with arrow functions, will be familiar to modern JS users as the ‘spread’ operator. The ‘…’ syntax allows us to unpack all the properties of array, which can be useful when creating new arrays which contain all the properties of pre-existing ones:

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

What have we gained?

Most of the features we’ve looked at our what’s known as syntactic sugar, which means essentially that they don’t allow us to do anything new but they provide a simpler and perhaps nicer syntax. These are, in my opinion, important additions to the language nonetheless as they allow developers to write cleaner and simpler code, which is easier to maintain as a result, and eliminates the need to write the same boilerplate time and again. These aren’t the only new features and I’d encourage everyone to look at the complete list on php.net.

Recent Blog Posts


How to improve your Google PageSpeed score with Webp images

If you're still serving images in long-time standard formats such as JPEG and PNG then a Google PageSpeed Insights analysis of your site will identify an opportunity to improve your page loading times by serving images in "next-gen" formats. There are several so-called "next-gen" image formats which offer superior compression to JPEG and PNG but in this post we will… Continue reading »

A puzzle with one piece missing

What are WebSockets?

What do WebSockets do? Hypertext Transfer Protocol (HTTP) is unidrectional, meaning requests can only be made by the client. This is a limitation which means that a certain class of web application cannot be built using HTTP alone. WebSockets are a separate communication protocol, fully compatible with HTTP, which allow for bidirectional communication between a client and a server. A protocol… Continue reading »