PHP 8.0 arrives with union types, JIT compilation

Major PHP upgrade also features named arguments and attributes, for including structured metadata in classes, functions, methods, and more

PHP 8.0 arrives with union types, JIT compilation
Thinkstock

PHP 8.0, a major upgrade to the popular dynamic language for server-side web programming, is now available as a production release, featuring union types, named arguments, attributes, and Just-In-Time compilation.

Union types accept values of multiple different types. They allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes arguments order-independent, and allows for arbitrarily skipping default values.

While PHP already supported two special union types (Type or Null and array or Traversable), arbitrary union types were not supported by the language, which instead has leveraged phpdoc annotations. After generics, union types have been considered the largest “hole” in the PHP type declaration system.

Support for union types enables moving more type information from phpdoc into function signatures, with advantages such as types being enforced and type information being less likely to become outdated or to miss edge cases. Also, types are checked during inheritance and are available through reflection. Syntax also is less boilerplate-like than phpdoc.

Released November 26 following five release candidates, PHP 8.0 can be downloaded from the php.net website. Other key capabilities in PHP 8.0:

  • Two JIT compilation engines are included, Tracing JIT and Function JIT. Tracing JIT shows about 3x better performance on synthetic benchmarks and 1.5x to 2x improvement on some long-running applications. Typical application performance is on par with PHP 7.4.
  • Similar to annotations in Java and attributes in languages such as C# and Rust, PHP attributes provide a form of structured, syntactic metadata to declarations of classes, constants, functions, methods, parameters, and properties. Attributes are specially formatted text enclosed with << and >> by reusing existing tokens T_SL and T_SR.
  • Named arguments allow passing arguments to a function based on the parameter name, instead of the parameter position. This makes the meaning of the argument self-documenting as well as making arguments order-independent and allowing the skipping of default values arbitrarily.
  • A nullsafe operator, ?->, with full short circuiting is featured, as a coding enhancement.
  • Weak maps enable creation of a map from objects to arbitrary values without preventing the objects used as keys from being garbage-collected. If an object key is garbage-collected, it will be removed from the map. The general use case for weak maps is to associate data with individual object instances without requiring that they stay alive and effectively leak memory in long-running processes.
  • Constructor property promotion introduces a short-hand syntax that allows the combining of the definition of properties and the constructor. This capability addresses a situation in which the definition of simple value objects has required a lot of boilerplate code, because all properties need to be repeated at least four times.
  • A match expression capability is added, similar to switch but with safer semantics and the ability to return values.

PHP 8.0 follows the PHP 7 line, which debuted in December 2015.

Copyright © 2020 IDG Communications, Inc.