The major upgrade brings named arguments, union types, attributes, and Just-In-Time compilation, where the union types feature accept values of multiple different types and passing arguments to a function based on the parameter name, instead of the parameter position.
Albeit, PHP already supported Type or Null and array or Traversable, arbitrary union types wasn't supported until now, but instead has leveraged phpdoc annotations. In turn, it makes arguments order-independent, and self-documenting, which allows for arbitrarily skipping of default values.
What's new in PHP 8.0?
Besides the support for union types, which enables moving more type information from phpdoc into function signatures, types are also checked during inheritance and are made available through reflection.
Other new features in PHP 8.0 includes:
- PHP attributes offer a form of structured metadata to declarations of classes, functions, methods, constants, parameters, and properties.
- Constructor property promotion allows the combining of the definition of properties and the constructor through a short-hand syntax. And it addresses a situation in which the simple value objects require a lot of boilerplate code, as a result of all properties needing to be repeated at least four times.
- Named arguments instead of the parameter position, this featute makes passing arguments to a function based on the parameter name.
- Tracing JIT and Function JIT Included: Tracing JIT perfoms 3x better on synthetic benchmarks and 1.5x to 2x on some long-running applications. Though, application performance is typically on par with PHP 7.4.
- Match expression capability, similar to switch but offers a safer semantics and ability to return values.
Additionally, there is the Weak maps, which enable creation of a map from objects to arbitrary values, with the objects used as keys prevented from being garbage-collected. The use case for weak maps is, however, to associate data with individual object instances without having to require that they stay alive and leak memory in long-running processes.
How to Get Started with PHP 8.0
PHP 8.0 follows after the PHP 7 line that made debut in December 2015. And it can be downloaded from the official php.net website.
You can check the supported versions page for more information on the support lifetime of each version of PHP.