PHP 8.1 Released With Enums, Read-Only Properties and Fibers
Written by Alex Armstrong   
Tuesday, 30 November 2021

PHP,  the server-side web language used for eight out of ten websites using server-side code, has an annual release cycle and the latest update to this venerable workhorse of the web has just been released.

Last year's release, see PHP 8.0 Adds Metadata To Classes, was a major release. While only a decimal point upgrade, PHP 8.1 has many new features, which is a remarkable feat for an open source language that has, until the formation of the PHP Foundation last week, been relatively under-supported.

Top of the list of new features is Enumerations (Enums) which allow a developer to define a custom type that is limited to one of a discrete number of possible values. The most popular example of enumerations is the built-in boolean type, which is an enumerated type with legal values  true and false.  Enums allows developers to define their own arbitrarily robust enumerations. phpenums

According to the release notes:

Enums appear in many languages with a variety of different features. In PHP, Enums are a special kind of object. The Enum itself is a class, and its possible cases are all single-instance objects of that class. That means Enum cases are valid objects and may be used anywhere an object may be used, including type checks.  

Next on the list is the readonly property modifier, which prevents modification of the property after initialization. This means they provide a good way to model value objects and data transfer objects.  

The third new feature, Fibers, addresses the problem that PHP code has until recently been synchronous, which can be slow. The introduction of Fibers seeks to eliminate the distinction between synchronous and asynchronous functions by allowing functions to be interruptible without polluting the entire call stack.

phpfibersAccording to the release notes:

Fibers are primitives for implementing lightweight cooperative concurrency. They are a means of creating code blocks that can be paused and resumed like Generators, but from anywhere in the stack. Fibers themselves don't magically provide concurrency, there still needs to be an event loop. However, they allow blocking and non-blocking implementations to share the same API.

Fibers allow getting rid of the boilerplate code previously seen with Promise::then() or Generator based coroutines. Libraries will generally build further abstractions around Fibers, so there's no need to interact with them directly.

PHP 8.1 also brings first-class callable syntax; intersection types; and the "never" return type for functions that never return but should either exit / throw an exception / not terminate. In addition the release introduces the final modifier for class constants; new fsync and fdatasync functions; a new array_is_list(), and explicit octal numeral notation. 

There are several performance-related improvements: 

  • JIT backend for ARM64 (AArch64)
  • Inheritance cache (avoid relinking classes in each request)
  • Fast class name resolution (avoid lowercasing and hash lookup)
  • timelib and ext/date performance improvements
  • SPL file-system iterators improvements
  • serialize/unserialize optimizations
  • Some internal functions optimization (get_declared_classes(), explode(), strtr(), strnatcmp(), dechex())
  • JIT improvements and fixes

As a result the release notes claim a speedup of 23% for the Symfony demo used as a benchmark compared to PHP 8.0 and and 3.5% for WordPress.

PHP 8.1 is now available for download.

phplogo

More Information

PHP Website

PHP 8.1.0 Released!

Related Articles

PHP 8.0 Adds Metadata To Classes

PHP Foundation Formed

PHP - Essential But Underappreciated

PHP Is Best?

What PHP Does

Ten Minutes to PHP

Ten Minutes to PHP Objects

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


NVIDIA Releases Free Courses On AI
19/04/2024

NVIDIA has jumped on the AI bandwagon in a big way. Hardware aside, this means working on training material too. Several self- paced courses have been released and for free too!



Eclipse JKube 1.16 Goes GA
08/04/2024

Eclipse JKube makes deploying your Java application to a Kubernetes cluster a breeze. Let's find out what's new.


More News

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Tuesday, 30 November 2021 )