PHP 8 is released: what's new in the major update

03 Jan 2021

What's new in PHP 8: overview of key features and improvements

A major update of a programming language is always a huge event in the software development world. One of these big days was November 26, 2020 — the PHP 8 release date. Our Drupal development team was happy to welcome the arrival of PHP 8.0.

It’s not just Drupal development teams that their breath at this release — PHP also lies at the heart of WordPress, Joomla, Magento, OpenCart, Craft CMS, PyroCMS, OctoberCMS, Contao, ExpressionEngine, and other CMSs. In addition, this open-source, platform-independent, powerful, flexible, fast, well-documented, and easy-to-learn programming language is used on websites without any CMS. In total, according to the stats, PHP works on 79.1% of websites whose server-side programming language is known.

The major changes and optimizations in the release deserve a good overview. Our team is ready to walk you through what’s new in PHP 8.

PHP 8: a brief introduction

It took 5 release candidates and the work of hundreds of people to have the PHP 8.0 version out on November 26. According to the PHP 8 roadmap, many long-expected and exciting features and improvements have arrived with the release. PHP 8 adds much more consistency, clarity, transparency, and efficiency to development, and gives developers a chance to build better, faster, and safer websites and applications.

Under the good, this is achieved by great syntax improvements, great core changes, significant API upgrades, numerous bug fixes, better approaches to error handling, and much more. Let's move on to more details about the most important PHP 8 changes.

  • JIT (Just-In-Time Compiler)

Among the improvements that deal with PHP 8 performance is the addition of the new PHP 8’s JIT compiler to the PHP core. For example, we can consider the results of the extensive speed tests performed by software engineer Pedro Escudero with and without JIT for different PHP versions. He discovered an impressive 45% performance improvement with the JIT Compiler.

This is especially useful for improving the performance of the CPU-heavy applications. It is very helpful for PHP as for an interpreted language that traditionally is interpreted line by line. On the contrary, the PHP 8 JIT compiles and caches some parts of the PHP code during runtime. This just-in-time compilation is also referred to as dynamic compilation. The JIT compiler is not enabled by default, but you can choose to enable it and configure for the ultimate results.

  • Union Types

One of the greatest PHP 8 news is that it supports union types. This enables developers to assign multiple types of values to a variable. This is similar to how it works in TypeScript. The main restriction for declaring union types in your classes or arguments is that you cannot use the void type. Prior to PHP 8, developers had to specify combinations of types in PHPDoc annotations.

  • Mixed Type

While discussing the union types support in PHP 8, we need to pay special attention to the new mixed type. Mixed is a pseudo type in PHP 8 that represents all types PHP works with. When you declare mixed, this means that the parameters, returns, and properties can be of any type. There is no need to exclude value type information. It is possible to use the mixed type in union with other types.

  • Constructor property promotion

Here is a PHP8 feature that makes the property declaration syntax more simplified, more readable, and less redundant. Traditionally, there is a lot of repetition during the class property declaration. This problem is addressed in PHP 8 — you can declare the visibility (public, protected, and private) and type of your class properties from the class constructor and these parameters are immediately assigned to the properties.

  • Named arguments

With named arguments in the PHP 8 release, developers can pass arguments to a function by adding the parameter name before its value. The parameter name instead of the parameter position becomes most important. The introduction of named arguments makes code easier to understand because the names of the arguments are descriptive. This feature also enables developers to only specify the required arguments without having to specify the default ones.

  • Attributes

Developers will greatly appreciate that the PHP 8.0 release also brings a new version of attributes. Earlier on, the only choice for adding metadata to declarations of classes, properties, etc., was through PHPDoc annotations. The new PHP 8 attributes are structured metadata that developers can use with the PHP native syntax. They can easily add them to the declarations of PHP classes, properties, methods, functions, function or method parameters, and constants.

  • Nullsafe operator

The new PHP 8’s nullsafe operator makes a developer’s life easier. They no longer need to remember to include a null check condition or, in case they forgot to do that or mistyped this condition, to see an unpleasant “Uncaught Error.” The new nullsafe operator does null checks for you and evaluates the whole chain to null if some element on the chain is null — no errors shown.

  • Match expression

The PHP8 release introduces a new match expression that is similar to the classic switch. Compared to switch, match does more strict checks. The new expression has a more concise syntax. It supports single-line expressions and allows you to specify multiple elements separated by a comma. There is no need to write the traditional “break” statement as it is added implicitly after every arm. The result of the match expression can be passed as argument or stored in a variable.

  • More PHP8 improvements

Among the new great things in PHP8 are saner string to number comparisons, better approaches to type system and error handling, stable sorting, as well as several new classes, functions, and interfaces introduced.

Notes on PHP 8 and Drupal

Of course, as a Drupal development team, we are especially interested in how things are going for PHP 8 and Drupal. The Drupal community has always had it as a priority to make the CMS compatible with the latest version of the programming language. According to the Drupal PHP requirement table, the supported PHP version from Drupal 8.8.3 to Drupal 9.0 was 7.4. But the newly released Drupal 9.1.0 is fully compatible with PHP8, which is an important benchmark.

Final thoughts

The time for greater websites and applications has come thanks to PHP 8. But this only works in professional hands. The Golems Drupal development team is always there for you whenever you need a hand with your Drupal website.

Reach out to us if you want to build a fast-performing website or add new features to your existing one. We can also help you update your PHP, update your Drupal 8 website to Drupal 9.1 so it has PHP8 compatibility, check if your code needs any fixes based on the deprecated features, or provide other support related to the new PHP8 release.

Always glad to hear from you,
your Golems

Comments

An
Anonymous
1 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
An
Anonymous
05 Tue 2021

Thanks for your blog post on php 8 versions features.

If had provided with more examples code, it would be so great.

Thanks.