PHP 8.1 Contributors
PHP 8.1 Contributors by php.watch

What’s new in PHP 8.1

25.11.2021 -exactly a year ago since the release of PHP 8.0 the version 8.1 is rolled out, and with it, new features and a significant speed increase.

From the point of a Webmaster that uses Laravel or WordPress, it’s important to note that 8.1 is still new, and not many plugins or libraries have been tested and marked compatible with the new version, so if you switch to PHP 8.1 make sure to detail test your website.

PHP 8.1 introduces Enums – one of the most requested features in PHP, and Fibers that are a significant step towards separating PHP from the traditional request-response pattern.

Here are some most notable features and improvements in PHP 8.1

Speed increase (Page rendering)

Thanks to multiple optimizations within PHP core, page rendering speed is significantly improved.

PHP states that the improvement is a staggering 23% on Symphony Demo App and 3.5% on WordPress (Aldo WordPress doesn’t yet officially support PHP 8.1).

Unlike PHP 8.0 these speed improvements don’t depend on the usage of the JIT (just-in-time) compiler but on the actual code of PHP 8.1.

the time needed for executing 25 x 250 requests/sec. on a Symfony Demo App

Author Dmitry Stogov also published that on 8.1 there is 5-8% increased performance for opcache module, a performance he calls “inheritance cache“. These features make caching links between classes possible, similar to preloading classes on version 7.4


New features in PHP 8.1

Faster page rendering speed is not the only new feature on PHP 8.1, there are dozens hundreds of new features, and here are just a few:

Enums

Enums (short for Enumerations) are countable types with fixed values. A good example for predefined Enums is a deck of cards, with 4 suits: Clubs, Diamonds, Hearts, and Spades.

enum Suit {
   case Clubs;
   case Diamonds;
   case Hearts;
   case Spades;
}

With Enum Suit we can now specify types when using the Suit value:

function pick_card(Suit $suit) {}
pick_card(Suit::Clubs);
pick_card(Suit::Diamonds);
pick_card(Suit::Hearts);
pick_card(Suit::Spades);

Fibers

Fibers or “green threads” are a very useful way of starting&stopping execution of a part of the code, making parallel execution possible. In the future, PHP frameworks such as Amphp and ReactPHP will benefit greatly from the use of Fibers.

Here is a great example from PHPwatch that shows how Fibers work.

We have a simple PHP script that sends 3 requests to a remote server, each request is shown in a different color: the colored part is the processing time and the I/O part is the time spent waiting for n response from the server.

Up until PHP 8.0 executing these requests looks like this:

With each request waiting for the previous to end: get a response and finish processing.

But in PHP 8.1 using Fibers this execution is simultaneous and therefore lowers the total time needed for processing all 3 requests.

As you can see in the picture above, while the first request is waiting for a response from the server, the second process is processing and therefore lowering the total execution time needed.

To be honest, in PHP 8.0 parallel processing is also possible by using yield and generators, but in PHP 8.1 this has a greater usage value as Fibers are built especially for this purpose.


Readonly properties

As the name suggests, readonly values can only be read, they are defined once and cant change their value.

Example:

class PostData { 
     public function __construct( 
         public readonly string $title,
         public readonly DateTimeImmutable $date, 
     ) {} 
}

If we try to change the Readonly property the following error is shown:

$post = new Post('Title', /* … */); 

$post->title = 'Other'; 

Error: Cannot modify readonly property Post::$title

Other notable features in PHP 8.1

The list of PHP 8.1 features goes on and on.. here are just a few of the msot notables:

  • never ype
  • final class constants
  • fsync i fdatasync functions
  • array_is_list functions
  • $_FILES: new full_path value for uploading files in directories
  • MYSQLI_REFRESH_REPLICA new MySQLi constant – simply repalcing the word slave with replica

Deprecated functions in PHP 8.1

And in the end, here is a list of deprecated functions, INI directives, and methods in PHP 8.1:

whoami
Stefan Pejcic
Join the discussion

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.