PHP 7.4 - Is the update worth it?

extendedLogo

PHP 7.4 was released at the end of November 2019 and is the last minor version before the release of PHP 8. What new features does PHP 7.4 bring? What are the advantages and when does it make sense to switch to PHP 7.4? We explore these questions in our article.

New features and improvements

The latest version of the scripting language brings with it a host of new features aimed at increasing the speed of code and simplifying its development. The new features also allow writing more readable code. We present some features/improvements below - a complete list of all innovations is available here.

Performance improvement

Phoronix has also extensively tested PHP 7.4 in comparison to the previous versions. Although no significant performance increases can be seen from version 7.3 to 7.4, the steady increase in performance in the versions is also noticeable and pleasing in this case.

PHP benchmark from Phoronix

PHP benchmark from Phoronix

All in all, this PHP upgrade also offers advantages for the performance of online shops. Due to the reduction of loading times sales can possibly be increased.

Weak References

Weak references allow you to create objects that can be removed by the garbage collector if necessary.

Example:

$o1 = new MyClass;

$r1 = new WeakRef($o1);

if ($r1->valid()) {

    var_dump($r1->get());
}

unset($o1);

if ($r1->valid()) {
    var_dump($r1->get());
}

Source: PHP Manual

This function is useful when you want to cache data, but you can remove it from memory when the garbage collector needs to free memory. The valid() method can be used to check whether the data is still in the memory or whether it has to be recalculated or loaded from a secondary memory. The advantage of this new function is that you can build a cache structure without additional services, which frees working memory when needed.

Preloading function

With the introduction of the so-called preloading function, it is possible to fill the PHP opcache already when starting PHP without having to execute the corresponding PHP files first. For this purpose, a new directive is included with opcache.preload, which allows you to specify a PHP script that is executed when PHP is started and fills the OPcache through includes or opcache_compile_file. PHP classes loaded in this way will remain in the OPcache until PHP is restarted. This means that changes to the PHP files have no effect until PHP is restarted. The new preloading function significantly improves the speed of PHP at startup. Details on this can also be found in the RFC.

As shown here in the example, the OPcache is filled by the file a.php. In this case the file b.php is loaded into the OPcache:

php.ini:
opcache.preload="/var/www/share/a.php"

a.php:
<?php
opcache_compile_file("/var/www/share/b.php");

However, if several applications are running on the same PHP server, this can lead to page effects, as the classes are loaded globally in each case. You should therefore pay special attention to possible page effects when using this functionality.

We are still evaluating whether we will provide an interface for opcache.preload in our Managed Center in the future. If you would like to use this function until then, please contact our Support.

FFIs

FFI (Foreign Function Interfaces) is a new extension that provides an easy way to call C functions, access C variables and create and call data structures defined in C libraries. By default, this function is only available for the command line and for preload scripts.

Example:

<?php
// create FFI object, loading libc and exporting function printf()
$ffi = FFI::cdef(
    "int printf(const char *format, ...);", // reguläre C Deklaration
    "libc.so.6");
// Rufe die C Funktion "printf()" auf
$ffi->printf("Hello %s!\n", "world");

Quelle: wiki.php.net

With the FFIs, it is now possible to write extensions in pure PHP and access external C libraries.

Spread Operator

This function - also called Argument Unpacking, is a shortened notation to merge parameters and is extended to arrays with PHP 7.4. This allows it to be used as an alternative to array_merge(). It is used by prepending three dots before an array or before a traversable instance.

Example:

$arr1 = [1, 2, 3];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]
$arr4 = array(...$arr1, ...$arr2, 111); //[1, 2, 3, 1, 2, 3, 111]
$arr5 = [...$arr1, ...$arr1]; //[1, 2, 3, 1, 2, 3]

function getArr() {
  return ['a', 'b'];
}
$arr6 = [...getArr(), 'c']; //['a', 'b', 'c']

$arr7 = [...new ArrayIterator(['a', 'b', 'c'])]; //['a', 'b', 'c']

function arrGen() {
    for($i = 11; $i < 15; $i++) {
        yield $i;
    }
}
$arr8 = [...arrGen()]; //[11, 12, 13, 14]

Source: wiki.php.net

The effect is a much shorter and clearer code, which significantly improves the readability of scripts. According to RFC documentation, improved performance also follows from the spread operator.

Typed properties

With PHP 7.4, it is now also possible to assign fixed types to variables, as is already known in many other programming languages.

This makes it possible to write shorter and better performing code, as in this example:

class Example {
    // All types except "void" and "callable" are supported.
    public int $scalarType;
    protected ClassName $classType;
    private ?ClassName $nullableClassType;

    // Types can also be applied to static properties.
    public static iterable $staticProp;

    // Types can also be set with the "var" notation.
    var bool $flag;

    // Default values are still possible.
    public string $str = "foo";
    public ?string $nullableStr = null;

    // If several variables are declared at the same time, the type applies to all variables
    public float $x, $y;
}

Source: wiki.php.net

Arrow function 2.0

Anonymous functions have been very cumbersome in PHP so far, even if they only perform a simple operation. This is partly due to a large amount of syntactic boilerplate and partly due to the need to import used variables manually. This makes code that uses simple closures hard to read and incomprehensible. The new Arrow notation for anonymous functions 2.0 now provides a shorthand syntax for defining functions, making code tidier and easier to read and maintain.

Example:

<?php
$y = 1;

//before
$fn2 = function ($x) use ($y) {
    return $x + $y;
};

//new Arrow notation
$fn1 = fn($x) => $x + $y;

Quelle: wiki.php.net

What must be considered before the update?

We recommend the following steps regardless of your shop or CMS system:

  • Check the compatibility of your shop or CMS system with the version
  • Create a backup
  • Set up the update on a development environment first so that you can run tests
  • Test plugins for compatibility
  • After successful testing, install the update in your live environment.

To increase the security and stability of your application, we always recommend the latest PHP version, all the more so since versions PHP 5.3 to PHP 7.1 no longer receive official security updates. PHP itself also supports this approach under the motto "Keeping Current". The advantages of this approach: security gaps are closed, the performance potential is increased, there are fewer problems when updating and using plugins and extensions, and the benefits of new features can be used immediately.

Compatibility of PHP 7.4 with common applications

Before you update your PHP version, you should check whether the new version is fully compatible with all components. The table shows which applications are already compatible with PHP 7.4 (as of June 2020).

Shopsystem Compatibility Notes
Magento 1 possible workaround
Magento 2 from version 2.4
Shopware 5 as of Shopware 5.6.5
Shopware 6 according to Shopware fForum
TYPO3 as of TYPO3 v10
WordPress as of WordPress 5.3
Drupal as of Drupal 8.9
Oxid
--- --- ---
Matomo (Piwik) as of Matomo 3.12.0
WooCommerce as of WooCommerce 3.8
xt:Commerce as of xt:Commerce 6.2.x; 7.4.x → Currently only with xt:Commerce FREE, as no ionCube Loader is available yet

EOL and roadmap of current PHP versions

If you cannot or do not want to decide on an update yet, it is also worth taking a look at the roadmap of the existing PHP versions. Official support for version 7.2 will expire at the end of this year. 7.3 will still be supported until the expected release of version 8.0. In addition to the official security updates, PHP versions 5.6 and higher currently receive unofficial security backports from Microsoft. These are also available on our clusters, but we recommend using a current PHP version if possible.

Version Security support
PHP 5.3 No more security updates
PHP 5.4 No more security updates
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 Official Security Support until 30.11.2020
PHP 7.3 Official Security Support until 06.12.2021
PHP 7.4 Official Security Support until 28.11.2022

Which applications and shop systems are compatible?

Magento 2 und PHP 7.4

Magento 2.4 supports the new PHP version. However, PHP 7.1 and 7.2 will be removed. The release of Magento 2.4 is expected in the 3rd quarter of 2020.

Update 28.07.2020 - Good news: Today Magento has released Version 2.4. This means that Magento 2 can now also be used with the new PHP version. Important: As already mentioned, support for PHP 7.1 and 7.2 ends with Magento 2.4.

Magento 1 and PHP 7.4

Currently, no version of Magento 1 is compatible with PHP 7.4. Since Magento 1 went EOL in June 2020, official support is no longer to be expected. However, the Magento fork OpenMage contains patches that allow Magento 1 to run with PHP 7.4.

Those who plan to continue with Magento 1 for a longer period of time might also find initiatives like Mage One interesting. In addition to patches for the latest versions of PHP, Apache, Nginx and MySQL, other services are offered here with which, according to Mage One, a Magento 1 shop can be operated professionally for a transitional period after the EOL.

Shopware 6 and PHP 7.4

Update 05.06.2020 - Shopware's forum confirms that Shopware 6 is compatible with PHP 7.4.

Shopware 5 and 7.4

Shopware has announced that there will be support for PHP 7.4 in version 5.6.5

Update 03.03.2020 - In the Changelog to Shopware 5.6 the compatibility is confirmed.

Wordpress and PHP 7.4

To ensure that all themes and plugins work with PHP 7.4, you should have made all updates to WordPress in advance. "WordPress continues to encourage all users to run the latest and greatest versions of PHP. This includes PHP 7.4 upon its official release." (Source: Wordpress) - while this implies that the current version of WordPress 5.3 is perfectly compatible with PHP 7.4, this does not always apply to the themes and plugins used.

Typo3 and PHP 7.4

Please note that only the current Typo3 version v10 is compatible with PHP 7.4. Please make sure that you have updated your CMS system to the latest version and that all plugins used support PHP 7.4 before updating to this version.

Profiler (Tideways, Blackfire, New Relic) and PHP 7.4

PHP modules compatible with PHP 7.4 are already available for Tideways as well as Blackfire IO and New Relic APM. maxcluster customers receive these automatically if the corresponding profiler has been activated via our Managed Center. In other cases, it may be necessary to update the plugin to receive PHP 7.4 support.

ionCube and PHP 7.4

With ionCube and the ionCube loader, it is possible to execute PHP code that has been obfuscated with ionCube. For more recent versions of Shopware, an ionCube is no longer necessary, but this is sometimes the case with other applications or plugins. An ionCube loader version that supports PHP 7.4 is expected in February.

Update: 08.06.2020 - Currently there is still no stable version, a second beta version but was already released at the end of February.

Switching to PHP 7.4: How to update successfully

Set up PHP 7.4 individually

If you are not using managed hosting, you can download PHP yourself. The further procedure depends on the operating system used:

For Debian and Ubuntu-based Linux distributions, we recommend the packages from Debian Maintainer Ondřej Surý https://deb.sury.org/. These versions are currently also used by maxcluster.

Set up PHP 7.4 at maxcluster

To support our customers, PHP 7.4 is already available on all our Ubuntu 18.04 clusters at maxcluster. To use PHP 7.4, it is sufficient to activate the version for the vHost. This process can be reversed if the application used has problems with the new version.

EnablePHP 7.4 for a vHost at maxcluster

If you are still using one of our older Debian clusters and want to use PHP 7.4, we will be happy to check the possibility of switching to an Ubuntu 18.04 cluster. Please contact our support team for this. Our team will also be happy to support you with questions and problems via e-mail to support@maxcluster.de or by phone at 05251/41 41 30.

Conclusion

With PHP 7.4, performance has been improved again. In addition, there are new code constructs that allow shorter and better structured code to be written. Online shops can therefore look forward to shorter loading and access times as well as improved performance. We therefore recommend switching to PHP 7.4 as soon as your application supports the version.

Although PHP 7.4 increases performance and significantly improves code readability, PHP 8 will be the real milestone for PHP performance, as the proposal to include a JIT compiler has already been approved (Note: PHP 8.0 was released on 26.11.2020 - all details can be found in our [related blog post*](https://maxcluster.de/en/blog/2020/11/php-8-update-change)). Here, similar to the JAVA JVM, PHP code is compiled into byte code before execution, which is then stored in the OPcache. This is expected to result in further strong improvements in PHP performance.

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