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

Some text from a computer terminal

Scheduling database backups with cron jobs

We're going to create a bash script which runs mysqldump (a MySQL backup program) to generate a database backup with a filename containing the date. We're then going to set up a cron job (a utility for running scheduled tasks) to run the script at regular intervals. We'll do all of this securely so that other users on the server can't… Continue reading »

Chess pieces

What is Game Theory?

Game Theory is a mathematical discipline developed by one of the most influential figures in computer science, John von Neumann. Simply put, it is the study of strategies of rational decision makers in games. From that description alone it may not be immediately obvious why that game theory is an influential field, or why it's even of interest to anyone. Game theory enables us to make predictions about how people will behave in games. The word "game" can be misleading, because the field isn't limited to conventional games but applies to all kinds of interactions between rational decision makers. Continue reading »