At the end of November, PHP released a new major version of its language :
Officially released to the public on 26 November 2020, PHP 8.0 adds a large number of optimizations and powerful features to the language. This particular update will enable you to write better code and build more powerful applications.
In addition to the many improvements that have been made to the language, this latest release also introduces new features :
Union types (documentation, RFC) :
Union types accept values that can be of different types. RFC union types 2.0 now proposes the addition of support for Union Types in function signatures so that the user is no longer dependent on the online documentation.
Named arguments (RFC) :
Named arguments provide a new way of passing arguments to a function in PHP. These allow arguments to be passed to a function based on the parameter name, rather than the position of the parameter.
Attributes that are inspired by annotations (documentation, RFC) :
Until PHP 7.4, doc-comments were the only means of adding metadata to declarations of classes, functions, etc. RFC Attributes v2 now brings attributes to PHP by defining them as a form of structured and syntactic metadata that can be added to declarations of classes, properties, functions, methods, parameters and constants.
Constructor property promotion, meaning it is now possible to indicate that a constructor parameter must be assigned to an attribute (documentation, RFC) :
Following an ongoing discussion on how to improve the usability of objects in PHP, the Constructor Property Promotion RFC proposes a new, more concise syntax that should simplify property declarations, making them shorter and less redundant.
The nullsafe operator, which allows method/attribute calls to be chained even if one element of the chain is null (RFC) :
This RFC introduces the $variable?-operator> with full short-circuit evaluation. With short-circuit evaluation, the second operator is only evaluated if the first operator does not evaluate to null. If an operator in a chain evaluates to null, the execution of the entire chain is aborted and evaluates to null.
WeakMap (documentation) :
PHP 7.4 has added support for weak references as a means of preserving a reference to an object without preventing the object itself from being destroyed. This is the reason why this RFC introduces a WeakMap class to create objects to be used as weak map keys; these can be destroyed and removed from the weak map if there are no other references to the key object..
Just in time (JIT) compilation :
According to the JIT RFC, the implementation of the « just in time » compilation engine is intended to improve the performance of PHP, but only on certain types of applications, specifically long-running applications.
Find out more !
More information about the PHP8 changes can be found (in French).
PHP 8.0 is available on Net4All ‘s Debian hosting solutions and can be used as a replacement for your current PHP version or on a new server.
Please don’t forget to take a look at the Guide to migrating PHP 7.4 code to PHP 8.0 and to check the compatibility of your framework and its dependencies to ensure a smooth upgrade.
Article written by Tristan Charbonneau, Linux Systems Administrator