PHP 8.2 – Worth the update?

09.02.2023
extendedLogo

On 8 December 2022, PHP 8.2, the new minor version of the PHP scripting language, was released. In this blog post, we show which innovations and changes are contained in PHP 8.2.

PHP 8.2, the new PHP minor release, already contains many changes and features that prepare for the next major version. At first glance, PHP 8.2 does not seem to bring a huge slew of new features. Nevertheless, this release also contains some interesting updates. In this article, you will learn whether and under what conditions it is worthwhile for shop operators to install PHP 8.2.

New features and improvements

In the usual PHP manner, PHP 8.2 delivers a lot of new functions and improvements. In addition to better performance, new features for classes, types and much more have been rolled out.

Performance

The performance of PHP 8.2 has improved slightly compared to the previous minor release PHP 8.1. This is already evident from the early benchmarks from Phoronix, which were carried out in May 2022.

Early benchmarks PHP 8.2 from Phoronix (PHP Benchmark Suite)Early benchmarks from Phoronix (PHP Benchmark Suite) I Source: Phoronix

Readonly Classes

Since PHP 8.1, class properties can be marked as read-only by tagging them with the keyword readonly. In PHP 8.2, it is now possible to mark entire classes as readonly. This has the advantage that all properties contained in the class inherit this flag. Since class properties no longer have to be marked readonly individually, the new feature saves a lot of time.

readonly class Test {
    public string $prop;
}

Example: Definition of a class with keyword readonly | Source: php.net

Classes without properties can also be marked as readonly. This prevents dynamic properties from being added to the class, while the properties of subordinate classes can be individually declared as readonly.

null and false as stand-alone types

With the major update PHP 8.0, the so-called union types were introduced. With the help of these, parameters and variables can be defined that can take on several possible types. In PHP 8.0, false and null can also be defined as parts of union types, but not as stand-alone types. The latter triggers an error message.

PHP 8.2 now makes it possible to define false and null as standalone types. Return, parameter and property types can be defined precisely in this way.

class Nil {
    public null $nil = null;

    public function foo(null $v): null { /* ... */ *}
}

class Falsy {
    public false $nil = false;

    public function foo(false $v): false { /* ... */ *}
}

null and false as stand-alone types | Source: php.net

True types

With the introduction of PHP 8.2, the true type is also established. This can be used wherever PHP allows the use of types. Thus, true can be used just like false and null as a stand-alone type or as part of union types.

class Truthy {
    public true $truthy = true;

    public function foo(true $v): true { /* ... */ *}
}

Example: true type as a stand-alone type | Source: php.net

Just like its counterpart false, the true type does not allow coercion. However, true and false cannot be defined together with the PHP type bool in a union type. In this case, errors would occur during compilation.

Constants in traits

Broken down to the essentials, traits are code that can be reused within different classes. Previously, only methods and properties could be defined within traits. As of PHP 8.2, this is now also possible with constants. A definition of trait constants is similar to the definition of constants within a class.

trait Foo {
    public const FLAG_1 = 1;
    protected const FLAG_2 = 2;
    private const FLAG_3 = 2;

    public function doFoo(int $flags): void {
        if ($flags & self::FLAG_1) {
            echo 'Got flag 1';
        }
        if ($flags & self::FLAG_2) {
            echo 'Got flag 2';
        }
        if ($flags & self::FLAG_3) {
            echo 'Got flag 3';
        }
    }
}

Example of a trait constant | Source: php.net

Trait constants can be called via the respective class in which the trait with the constant is used. However, access to the constant via the defined properties is not possible.

New random extension

With the random extension, a new random generator is implemented in PHP 8.2, which functions much more efficiently and reliably than its predecessor. The new PHP class Randomizer can contain such a random generator engine. Existing PHP functions for generating random numbers are organised in the new extension and extended with new PHP class structures.

Disjunctive normal form types

The newly introduced DNF types are based on the DNF method used to organise Boolean expressions. DNF types allow intersection types and union types to be combined so that they can be processed by the parser. The default for such a combination is that the Intersection Types must be grouped using parentheses.

interface A {}
interface B {}
interface C extends A {}
interface D {}

class W implements A {}
class X implements B {}
class Y implements A, B {}
class Z extends Y implements C {}

Example of a DNF type with already existing interface and class definitions | Source: php.net

New MySQLi extension

Before the release of PHP 8.2, executing parameterised SQL queries was only possible using a lot of boilerplate code. The new MySQLi extension, which includes the new mysqli_execute_query function as well as the new mysqli::execute_query method, makes it much easier to retrieve results from an SQL query.

The function mysqli_execute_query is composed of the functions mysqli_prepare(), mysqli_execute() and mysqli_stmt_get_result(). In this way, the MySQL query is prepared, bound and executed within the function. If the query is successful, a mysqli_result object is also output. If, on the other hand, the query is not successful, false is output.

Further innovations

PHP 8.2 contains numerous other features in addition to those already mentioned. These are some of them:

  • Sensitive parameters in backtraces: Stack tracing helps when debugging code and has been possible in PHP for some time. In PHP 8.2, sensitive parameters can be provided with the attribute SensitiveParameter. This removes them from the stack trace.
  • New OpenSSL function: The OpenSSL extension has a new function openssl_cipher_key_length. This returns the required length of the key for each supported OpenSSL cipher.
  • Upkeep function in curl extension: The new curl_upkeep function assists in maintaining a curl connection. This function is currently only supported by HTTP/2.

Deprecations

In PHP 8.2, initial preparations are already being made with regard to the next major release, PHP 9.0. Thus, some PHP functions and modalities are considered deprecated with the release of PHP 8.2 and will no longer be supported.

Dynamic class properties

Within PHP classes, it was previously possible to dynamically set undeclared class properties. While this feature offers the flexibility to create classes without a strict class definition, there is also the risk of errors occurring within the application. In PHP 8.2, setting a value for an undeclared class property is therefore discarded.

MySQLi compilation with `libmysqli

Previously, MySQL databases could be compiled with one of the two libraries mysqlnd or libmysqli. Since PHP 5.4, mysqlnd is already set as a library by default. In PHP 8.2 and future PHP versions, MySQLi compilation with libmysqli is no longer supported.

utf8_encode and utf8_decode

The utf8_encode and utf8_decode functions are considered deprecated in PHP 8.2. Previously, they were part of the standard PHP library and were used to convert strings encoded in ISO-8859-1 or UTF-8.

However, they cannot be used to convert other common strings. Therefore, errors often occur when entering unsupported strings. Instead of the functions mentioned, the use of the PHP extensions mbstring, intl or iconv is recommended.

What to consider before updating?

Are you thinking about updating to the new PHP version 8.2? You should check the following things before you start the update:

  1. check whether your shop or CMS system is already compatible with PHP 8.2.
  2. make sure to create a backup before the update so that you can restore the original state in the event of problems.
  3. Test the new PHP update in a development environment to identify possible problem scenarios in advance. These practical tests give you more security for the update in your productive environment. By the way, your plug-ins should also be tested for compatibility with PHP 8.2.
  4. You can use the PHP migration guide to determine whether changes need to be made to your own code in the course of the update.
  5. If your tests are successful, you can install the update in your production environment.

Increase the security and stability of your application by always using the latest PHP version, if possible. Otherwise, use a version that receives official security updates if possible. 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.2 with popular applications

The new features and enhancements of PHP 8.2 have convinced you and you want to migrate your application to the new version immediately? Since PHP 8.2 is still fairly new, there is still no information available regarding the compatibility of all common applications. We will provide you with this information successively in this blog article by regularly updating the following table. You can use the table to check whether the new version is compatible with your application:

Shop system / CMSCompatibilityNote
Magento 1Official support is not to be expected, as Magento 1 is already end-of-life
Magento 2PHP 8.2 is supported as of Magento 2.4.6
Shopware 5PHP 8.2 support as of version 5.7.17
Shopware 6PHP 8.2 support as of Shopware 6.5
Typo3Support for PHP 8.2 for Typo3 v12 and v11 LTS
WordPress / WooCommercePHP 8.2 support for WordPress 6.1
DrupalSupport for PHP 8.2 as of Drupal 10.0.0
OroCommerceSupport for PHP 8.2 as of OroCommerce 5.1 LTS
Further applicationsCompatibilityNote
Matomo (Piwik)Matomo is meanwhile compatible with PHP 8.2
TidewaysTideways is already compatible with PHP 8.2

Which applications and shop systems are compatible?

Here you can find more details on the (planned) compatibility of PHP 8.2 with common shop and CMS systems. This article will be updated continuously so that missing information will be added as soon as it is known.

Magento 2 and PHP 8.2

Magento 2 supports PHP 8.2 as of version 2.4.6, which was released on March 2023.

Magento 1 and PHP 8.2

Due to the end-of-life of Magento 1, it can be assumed that there will be no official support for PHP 8.2. Within the framework of the Magento work "OpenMage Magento LTS", work is currently being done on support for PHP 8.1. Corresponding patches may also be available soon through the "Mage One" project.

Shopware 6 and PHP 8.2

Shopware 6 supports PHP 8.2 as of version 6.5, which was released in May 2023.

Shopware 5 and PHP 8.2

Shopware 5 is already compatible with PHP 8.2 as of version 5.7.17.

WordPress / WooCommerce and PHP 8.2

There is already support for PHP 8.2 in WordPress 6.1.

Typo3 and PHP 8.2

Typo3 is already compatible with PHP 8.2 in versions v12 and v11 LTS.

EOL and roadmap of current PHP versions

An immediate change to PHP 8.2 is of course not mandatory, but you should still keep an eye on the roadmap of the current PHP versions. This way you can carefully plan your next steps without being surprised by the sudden EOL of a PHP version. Some versions still receive unofficial security backports, which are also available on our clusters. However, we recommend using a version that is as up-to-date as possible. For example, official support for PHP 7.4 has recently expired.

VersionSecurity-Support
PHP 7.0Unofficial Security Backports
PHP 7.1Unofficial Security Backports
PHP 7.2Unofficial Security Backports
PHP 7.3Unofficial Security Backports
PHP 7.4Unofficial Security Backports
PHP 8.0Official Security Support until 26.11.2023
PHP 8.1Official Security Support until 25.11.2024
PHP 8.2Official Security Support until 24.11.2025

Switching to PHP 8.2: How to update successfully

Individual setup of PHP 8.2

If you do not use Managed Hosting, it is also possible to set up PHP 8.2 individually. To do this, first download the current version. Depending on your operating system, you can then proceed as follows:

Set up PHP 8.2 at maxcluster

At maxcluster, PHP 8.2 will be available for installation on all Ubuntu clusters from 01.02.2023. If you want to use the new version for your application, it is sufficient to activate the version for the vHost in our Application Center. If the application used in combination with PHP 8.2 should lead to problems, the process can be reversed immediately.

Installation of PHP 8.2 in the Application Center of maxclusterInstallation of PHP 8.2 in the Application Center of maxcluster I Screenshot: maxcluster

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

Conclusion

Just like its predecessors, PHP 8.2 contains many useful features and improvements. The next major version is also already casting its first shadows. For example, some established PHP functions have been declared obsolete in the course of the new release. If you are planning to switch to the new version, check beforehand whether you are using one or more of the deprecated PHP functions within your application.

Currently, PHP 8.2 is not yet supported by all common shop and CMS applications. It is not advisable to switch until the respective provider announces official support for the new PHP version.


Last updated on 10.05.2023 | 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