PHP 8.4 released with property accessors after a “wild ride” – plus lazy objects and new DOM namespace

PHP 8.4 released with property accessors after a “wild ride” – plus lazy objects and new DOM namespace

PHP 8.4 was released last week, with long-awaited richer object-orientation, HTML5 DOM support and other new features.

This latest version of PHP introduces property hooks – known as accessors in some languages – and described in the new feature guide as additional logic associated with get and set operations for object properties. This includes virtual properties, which have no backing variable but may be calculated from other properties, as well as the ability to control set visibility separately from get visibility.

It is also now possible to create lazy objects – objects that are not initialized until they are accessed.

Object orientation came late to PHP, whose first version, released in the mid-1990s, was not even intended to be a full programming language. It was not until 2004 and PHP 5.0 that the core features of object orientation were implemented. Property accessors have been in other languages, such as Java and C#, for many years.

PHP contributor Larry Garfield noted that proposals for adding property accessors to PHP have been made as far back as 2009 but failed to get support. The new feature is based on the same model as that in Swift and according to Garfield it was difficult to get it approved. Some on the development list, he said, “liked the idea, couldn’t find fault with the implementation, but were concerned about just how big and complex the feature was.”

The feature was eventually passed. “PHP now joins the cadre of languages with robust, flexible properties … it’s been a wild ride,” said Garfield.

Another big feature in PHP 8.4 is improved DOM support, for working with HTML 5. There is a new DOM namespace – created because, according to the proposal, “there are many bugs in PHP’s DOM extension.”

These bugs are long-standing, such that the PHP developers were aware of a ton of code out there that relies on them. “Two decades of buggy behavior have become deeply ingrained in the system,” the RFC (Request for Comments) states. Therefore, the old functions remain in place, with the DOM namespace used for an improved version which should work well with HTML 5.

While these improvements are welcome, they also illustrate the lack of sophistication of PHP versus other modern languages. 

Nevertheless, PHP powers a large proportion of the web, thanks to its use in sites powered by WordPress, Drupal, and many other custom web applications.