Menu
PHP 8.0 arrives with union types, JIT compilation

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

Credit: Dreamstime

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.


Follow Us

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

Featured

Slideshows

The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards

The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards

Hundreds of leaders from the New Zealand IT industry gathered at the Hilton in Auckland on 17 November to celebrate the finest female talent in the Kiwi channel and recognise the winners of the Reseller News Women in ICT Awards (WIICTA) 2020.

The Kiwi channel gathers for the 2020 Reseller News Women in ICT Awards
Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards

Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards

The leading female front runners of the New Zealand ICT industry joined together for the annual Reseller News Women in ICT Awards event at the Hilton in Auckland, during which hundreds of guests celebrated 13 outstanding individuals who won awards, chosen from more than 50 finalists representing over 30 organisations.

Leading female front runners honoured at the 2020 Reseller News Women in ICT Awards
Channel gathers to celebrate the Reseller News Innovation Awards 2020 winners

Channel gathers to celebrate the Reseller News Innovation Awards 2020 winners

More than 500 channel leaders gathered in Auckland on 21 October at the ​Reseller News Innovation Awards ​2020 to celebrate the achievements of the New Zealand technology industry's top partners, start-ups, vendors, distributors and individuals.

Channel gathers to celebrate the Reseller News Innovation Awards 2020 winners
Show Comments