Experts insights Updates and releases

PHP 8.1 now available: what’s new in this update?

The gandi.net logo in dark blue superimposed over the PHP 8 logo (a stylized figure 8 in black and white), on a neon green background

On November 25, 2021, the PHP development team announced the immediate availability of PHP 8.1, a major release. In its announcement addendum, php.net proudly claims the update will provide “better performance, better syntax, improved type safety.”

Here is a summary of what we feel are the most notable aspects of this release.

Performance improvements

Thanks to several optimizations in the PHP code, page rendering speed has been improved. PHP announced improvements of around 23% on page rendering using the Symphony Demo App and 3.5% for WordPress (although its latest release does not yet support PHP 8.1). It’s important to note, though, that these improvements do not depend on the use of JIT (just-in-time) compiler, so you will see improvements as soon as you start using PHP 8.1.

Changes for developers

Enumerations

Enumerations are used whenever you need a type that provides a fixed, limited number of options and allows them to be conveniently named. Here’s a quick example to help explain:

enum Language {

    case JavaScript;

    case PHP;

    case Python;

}

$current_language = Language::PHP;

This can be used with “match“ expressions that were introduced in PHP 8.0:

echo match ($current_language) {

  Language::PHP => "Hello World!",

  default => "",

};

// Hello World!

First Class Callable Syntax

Contrary to certain other languages in which functions are standard objects, in PHP you can’t just use a function reference to store it into a variable (“$var = strlen;“ it not valid). You can now use “function_name(…)“ (where the dots are part of the syntax) to get an anonymous function as in:




$rev = strrev(...);

echo $rev("PHP");

// PHP

echo strrev(...)("idnaG");

// Gandi

This enables you use avoid using, e.g. “Closure::fromCallable“ and creates a shorter and clearer solution.

String-keyed array merging through unpacking

In PHP, arrays can be used for what other languages may call lists (an ordered collection of values) or for dictionaries or hashes (a collection of (key, value) items). PHP already supported merging numeric-indexed arrays as in:

$array1 = [1, 2];

$array2 = [3];

$merged = [0, ...$array1, ...$array2];

// [0 => 0, 1 => 1, 2 => 2, 3 => 3]

This has been extended to support merging arrays that have string keys:

$merged = [0, "a" => "A", ...[1, "a" => "a"]];

// [0 => 0, "a" => "a", 1 => 1]

“Readonly“ property

Readonly properties (“readonly“ keyword) allow creating objects with properties that can be set when the object is created (inside object constructor method) but cannot be changed afterwards. They differ from constants (“const“ keyword) which are set directly when the class is defined.

Final class constants

Class constants can now be made final (using the “final“ keyword) to prevent classes that inherit from them to change the constant value. “final“ is not a new keyword as it was already usable for methods and class methods.

Other notable changes

Some other changes have been introduced in PHP 8.1:

  • a new notation for octal numbers: The “0o“ prefix can now be used (both “0o10“ and “010“ are 8, “0o1_000“ is 512)
  • “never“ return type to indicate a function should never return (and rather exit or throw an exception)
  • “new“ keyword can be used as default parameter and more generally in initializer context
  • intersection types using “&“ keyword that can be used to force a parameter to satisfy the constraints of multiple types
  • fibers to simplify writing concurrent code when code is executed in an event loop

For more information on how to migrate code to PHP 8.1, you can refer to the official documentation.
And to actually host code with a PHP 8.1 interpreter, feel free to use our Simple Hosting service.