PHP 8.1 - Worth the update?

30.11.2021
extendedLogo

On 25 November 2021, almost exactly one year after the major release of PHP 8.0, the latest version of the scripting language, PHP 8.1, was released. This release also brings with it a whole host of new features and improvements. But when and under what conditions does it make sense to switch to PHP 8.1?

We have compiled the most important information in the following blog post. Since experience shows that many applications will only receive direct support for PHP 8.1 in the following period, we will continuously update this article over the next few weeks.

The new version PHP 8.1

November 2021 is shaping up to be a month of news in the PHP cosmos. In addition to the release of PHP 8.1, the establishment of the PHP Foundation was announced. This is a non-profit umbrella organisation that monitors and guarantees the independent (further) development of the PHP programming language. One of the active supporters of the PHP Foundation is our partner Tideways, which also offers PHP 8.1 support for its software solution of the same name.

With the release of new PHP versions, the general aim is always to make the scripting language more performant and secure and to expand its areas of application. It is therefore worthwhile to always keep the code of a website based on PHP up to date and to use as new a PHP version as possible.

Please note that you may need to make adjustments to your code due to the changes in PHP 8.1. Although this is a minor release, some changes may not be backwards compatible. A list of all necessary adjustments can be found in this migration guide.

New features and improvements

PHP 8.1 continues the tradition of the previous versions and again brings a lot of new features and improvements. In the following, we present some of them in detail. An overview of all implemented features can be found here.

Performance

The Early Benchmarks from Phoronix, which were carried out in July 2021, already show that PHP 8.1 again brings small performance improvements in direct comparison with the previous versions. Compared to PHP 8.0, the results still show an improvement of three percent.

Performance improvement with PHP 8.1 (PHP Benchmark Suite)Performance improvement with PHP 8.1 (PHP Benchmark Suite) I Source: Phoronix

Performance improvement with PHP 8.1 (Zend Bench)Performance improvement with PHP 8.1 (Zend Bench) I Source: Phoronix

More performance through Opcache improvement

In PHP 8.1, performance improvements have also been made to the opcache. Called Inheritance Cache, the feature now allows all uniquely dependent classes (e.g. methods, interfaces, traits, etc.) that previously had to be compiled separately to be linked together and cached in opcache shared memory. According to developer Dmitry Stogov, this feature can improve performance by five to eight percent. This performance optimisation can be used without changing the programme code.

Enums

With Enums (short for Enumerated Types), PHP 8.1 gets direct support for data types which can take on one of several fixed values. This construct is already familiar to many developers from other programming languages. Enums are implemented in PHP classes and therefore inherit many class semantics.

Enums support functions such as namespaces and autoloading. They can also define methods or be used to extend classes or implement interfaces. However, it is not possible to derive child classes from an enum.

Enums also establish a new syntax similar to that of traits, classes and interfaces. In their basic form, enums are first marked with the keyword enum, followed by their respective names. This is followed by the possible values, each of which is introduced with the keyword case.

enum Suit {
  case Hearts;
  case Diamonds;
  case Clubs;
  case Spades;
}

Example: Pure Enum I Source: PHP Wiki

In addition to these so-called Pure Enums, PHP 8.1 also allows the use of Backend Enums. With these, an internal string or integer value is assigned to each possible value that the respective enum can accept.

enum Suit: string {
  case Hearts = 'H';
  case Diamonds = 'D';
  case Clubs = 'C';
  case Spades = 'S';
}

Example: Backed Enum I Source: PHP Wiki

A Backed Enum can contain either the type int or the type string. However, a mixture of both types within the enum is not possible. An enum is either a pure or a backed enum. Therefore, either string or integer values must be assigned to all possible values of an enum or to none of these values.

Fibers

With the new PHP function Fibers (also known as green-threads), selected parts of a programme can be started, stopped or terminated in isolation. The new PHP class 'Fibers' allows the implementation of a fibre. Fibers are blocks of code that have their own stack (variables and states).

A fibre is started by the main thread, but cannot be interrupted by it during execution. After a fibre has been stopped or terminated, it can only be restarted via the main thread.

With fibers themselves, it is still not possible to execute code in parallel, since a called fiber pauses the actual PHP execution. Fibers are therefore primarily intended as a low-level interface for framework developers. Through these frameworks, functions can then in turn be made available for asynchronous development with PHP.

Fibers function according to the "concurrent execution" modelFibers function according to the "concurrent execution" model I Representation according to: php.watch

fsync and fdatasync

With fsync and fdatasync, PHP 8.1 receives two new functions that complement the group of file system functions. Both functions are similar to the existing function fflush, which is used in the context of write operations (fwrite) to ensure the transfer of PHP's internal buffers to the operating system.

/**
 * @param resource $stream
 */
function fsync($stream): bool {}

Example: fsync I Source: php.watch

/**
 * @param resource $stream
 */
function fdatasync($stream): bool {}

Example: fdatasync I Source: php.watch

In addition, fsync and fdatasync send requests to the operating system to write the buffers to the storage medium. While fsync synchronises file changes including metadata, fdatasync is used to synchronise file changes without metadata. The use of these functions is particularly useful in applications designed for persistent data storage to ensure data integrity.

Final Class Constants

Subclasses are able to override constants set in PHP classes. With PHP 8.1, it is now also possible to mark class or interface constants with final. Constants marked final can no longer be overwritten or extended by subclasses.

class Foo
{
    final public const X = "foo";
}

class Bar extends Foo
{
    public const X = "bar";
}

Example: Final Class Constants I Source: PHP Wiki

never Return Type

never is a new return type. Functions that have the keyword never indicate that never a value is returned. The function is therefore not terminated in the regular way. Instead, an exception is thrown or the programme is terminated by an exit call.

function redirect(string $url): never {
    header('Location: ' . $url);
    exit();
}
function redirect(string $uri): never {
    header('Location: ' . $uri);
    exit();
}

function redirectToLoginPage(): never {
    redirect('/login');
    echo 'Hello'; // <- dead code detected by static analysis
}

Example: never Return Type I Source: php.net

never is similar to the already existing return type void, because it also specifies that a function never returns a value. However, unlike never, void does not terminate the program flow, so PHP will continue to execute the next statement after the function call.

Readonly Properties

PHP 8.1 makes it possible to mark class properties as read-only. If the keyword readonly is implemented in a typed property, it cannot be changed after initialisation. Read-only property values can only be set within the class itself.

class Test {
    public readonly string $prop;

    public function __construct(string $prop) {
        $this->prop = $prop;
    }
}

Example Readonly Property I Source: PHP Wiki

array_is_list

PHP arrays contain either integer or string keys and are very flexible in their application possibilities. They can also be used to represent more extensive structures. Until now, however, there was no feature that could be used to check whether a particular array was a list.

The new function array_is_list does exactly this by checking the contained array keys starting with the value 0 with regard to the presence of a numerical order. Depending on whether this is true or not, the function outputs one of the boolean values true or false. With the help of array_is_list, potential sources of errors, which can even lead to significant performance problems, can be circumvented.

array_is_list([]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list([0 => 'apple', 'orange']); // true

// The array does not start at 0
array_is_list([1 => 'apple', 'orange']); // false

// The keys are not in the correct order
array_is_list([1 => 'apple', 0 => 'orange']); // false

// Non-integer keys
array_is_list([0 => 'apple', 'foo' => 'bar']); // false

// Non-consecutive keys
array_is_list([0 => 'apple', 2 => 'bar']); // false

Example: array_is_list I Source: PHP Wiki

Further innovations

In addition to the features already described, PHP 8.1 has a number of other innovations to offer. Here is an overview of some of them:

  • Pure Intersection Types are similar to the Union Types introduced in PHP 8.0 and are especially useful for applications that interact with many interfaces. Array Unpacking is now also possible with string keys. New In Initializers allow new keywords to be used as default parameters in function definitions and other places.
  • The new First-Class Callable Syntax simplifies the creation of callables with the same syntax used to call methods and functions.
  • The GD extension in PHP 8.1 now includes support for the AVIF image format. Before using it, GD must first be compiled with AVIF support.
  • The option to display FPM status in openmetrics format allows for easier interfacing with monitoring tools such as Prometheus.

Here is the full list of changes.

What do I need to know before updating?

The new features of PHP 8.1 are interesting for you and you are thinking about an update? Regardless of your shop or CMS system, you should definitely consider the following things before updating:

  1. check whether your shop or CMS system is (already) compatible with PHP 8.1.
  2. security is paramount: therefore, make sure to create a backup before you start the update.
  3. Set up a development environment and install the PHP update there. By running tests, you can check the update in practice and get even more security. Don't forget to also test your plug-ins for compatibility.
  4. Check your own code for necessary changes using the PHP Migration Guide.
  5. If your tests were successful, there is nothing to stop you from installing the update in your live environment.

Increase the security and stability of your application by - if possible - always using the latest PHP version. If this is not possible, use a version that receives official security updates. The more up-to-date your PHP version is, the higher the performance potential of your application will usually be. You will also have fewer problems regarding the use of features and extensions when using plugins.

Compatibility of PHP 8.1 with popular applications

The new features and enhancements of PHP 8.1 have convinced you and you want to migrate your application to the new version immediately? You can use the table to check whether the PHP 8.1 is compatible with your application:

Shop system / CMS Compatibility Note
Magento 1 Official support is not to be expected as Magento 1 is already end-of-life
Magento 2 Support for PHP 8.1 as of version 2.4.4
Shopware 5 Support for PHP 8.1 as of version 5.7.7
Shopware 6 (✓) Support of PHP 8.1 as of version 6.4.9.0 (as of Professional Edition)
TYPO3 Support for PHP 8.1 as of version v11.5.3
WordPress / WooCommerce (✓) Beta support for PHP 8.1 as of version 5.9
Drupal Both Drupal 9 and Drupal 10 are now compatible with PHP 8.1
Oro Commerce Oro Commerce is now compatible with PHP 8.1
Further applications Kompatibilität Note
Matomo (Piwik) Matomo is now compatible with PHP 8.1
Tideways Support for PHP 8.1 as of version 2021.3

Which applications and shop systems are compatible?

Here you will find more details on the (planned) compatibility of PHP 8.1 with common shop and CMS systems. We will provide you with the missing information as soon as it is available.

Magento 2 and PHP 8.1

Magento 2 supports PHP 8.1 starting with version 2.4.4, which was released on April 12, 2022.

Magento 1 and PHP 8.1

Due to the end-of-life of Magento 1, it cannot be assumed that there will be official support for PHP 8.1. Support for PHP 8.0 is currently being worked on as part of the Magento work "OpenMage Magento LTS". Corresponding patches may also soon be available through the "Mage One" project.

Shopware 6 and PHP 8.1

With the release of version 6.4.9.0 on 14 March 2022, compatibility with PHP 8.0 and 8.1 was established. However, this is currently only available in the Professional and Enterprise Editions of Shopware 6. We firmly expect that PHP 8.1 will also be compatible with the Community Edition in the near future.

Shopware 5 and PHP 8.1

With the update to version 5.7.7, which was released on 05 January 2022, Shopware 5 is compatible with PHP 8.1.

WordPress / WooCommerce and PHP 8.1

Wordpress and WooCommerce offer beta support for PHP 8.1 starting with WordPress version 5.9. There is therefore basically compatibility, but problems may occur after installation.

TYPO3 and PHP 8.1

The current TYPO3 version v11.5.3, which was released on November 16, 2021, is already compatible with PHP 8.1.

EOL and roadmap of current PHP versions

Of course, you don't have to switch to PHP 8.1 yet, but you should still take a look at the roadmap of current versions to plan your next steps. Some versions still receive unofficial security backports, which are also available on our clusters. However, we recommend using a current version if possible. In particular, the official support for PHP 7.4 will expire in this year.

Version Security-Support
<= PHP 5.5 No more security updates
PHP 5.6 Unofficial security backports
PHP 7.0 Unofficial Security Backports
PHP 7.1 Unofficial Security Backports
PHP 7.2 Unofficial Security Backports
PHP 7.3 Unofficial Security Backports
PHP 7.4 Official Security Support until 28.11.2022
PHP 8.0 Official Security Support until 26.11.2023
PHP 8.1 Official Security Support until 25.11.2024

Switching to PHP 8.1: How to update successfully

Set up PHP 8.1 individually

If you do not use Managed Hosting, you can also set up PHP 8.1 individually. To do this, download the current version in advance. The further procedure depends on the operating system you are using:

Debian and Ubuntu: For these Linux distributions, we recommend the packages from Debian Maintainer Ondřej Surý, which we also use at maxcluster.

RPM: For Linux distributions based on RPM, the packages from Remi can be used.

Windows: For development environments under Windows, again the packages from Ondřej Surý at WSL can be used. Alternatively, you can create a simple development environment with XAMPP.

Set up PHP 8.1 at maxcluster

At maxcluster, PHP 8.1 will be available from 30.11.2021 on all Ubuntu clusters. If you want to use it for your application, it is sufficient to activate the version for the vHost in our Managed Center. If the deployed application has problems with this new version, the process can be reversed immediately.

Activation of PHP 8.1 in the Managed CenterActivation of PHP 8.1 in the Managed Center I Source: maxcluster

Are you still using one of our older Debian clusters and would like to use PHP 8.1? If you would like to check whether it is possible to switch to an Ubuntu cluster 18.04 or 20.04, please feel free to contact our support at any time. Our team will also be happy to support you with other questions and problems via e-mail to support@maxcluster.de or by phone at 05251/41 41 30.

Conclusion

PHP 8.1 comes with a lot of useful features and improvements. Many developers are already certain that the new version will again make a significant leap forward in terms of power and performance in direct comparison with PHP 8.0. Especially for online shops, the upgrade to PHP 8.1 can be very worthwhile, as the improved performance results in shorter loading times and more stability.


Last updated on 29.09.2022 | DR

You have questions, requests, criticism, suggestions or just want to tell us your opinion about our blog? Here you have the opportunity to contact us directly.

Send e-mail