PHP 8: release features and improvements

Release PHP 8 (PHP major update)

The last version of the PHP language, PHP 8, was released on November 26, 2020. This last major update includes many new features and significant optimizations, compared with previous versions. The development of the 8th version of PHP has been focused on creating new functionalities. Named arguments, attributes, constructor properties and JIT are some of its new major features.

Named arguments

Named arguments are order-independent and self-documented, and enable skipping optional parameters. They allow naming parameters when calling a function; a feature already exists in other programming languages. 

Named arguments enable developers to just overwrite the default values they wish to change. This way, there is no longer need to specify all the default values. Besides, named arguments can be combined with normal, positional arguments.

JIT: Just-In-Time Compilation

As part of the improvements in performance, PHP 8 includes two JIT compilation engines: Tracing JIT and Function JIT. Typical application performance is similar to the previous PHP version, but performance for CPU-intensive applications is boosted. As explained in the JIT PHP RFC:

“Using JIT may open the door for PHP being more frequently used in other, non-Web, CPU-intensive scenarios – where the performance benefits will actually be very substantial – and for which PHP is probably not even being considered today.”

JIT compilation, also known as “dynamic translation”, is a useful technique to significantly improve performance. It consists of executing computer code that’s directly compiled during the execution of a program; it is to say, at run time instead of before the execution.

Attributes

This new feature allows adding structured metadata directly with PHP’s native syntax; without using PHPDoc annotations. Attributes can be applied to classes, functions, interfaces. etc. A similar concept to attributes already exists in other programming languages.

Union types

Union types allow moving more type information from PHPDoc annotations to function signatures. Supporting union types directly in the language brings diverse advantages, such as: significantly simplifying and optimizing the syntax, minimizing the risk of type information becoming outdated and early mistake detection. Nevertheless, arbitrary union types aren’t supported for now.

Further features of PHP 8

PHP 8 includes further features such as the constructor property promotion RFC, match expressions or the nullsafe operator. It also includes type system and error handling improvements, such as: static return type, reclassified engine warnings or stricter type checks for arithmetic and bitwise operators.

This new version also includes new functions, classes and interfaces, such as: token_get_all(), get_debut_type(), a weak map class or a new stringable interface.

You can find further details, examples and documentation about PHP 8 on its release webpage.

Share it on Social Media!