PHP 8.1

PHP 8.1 is Here Studded with Highly Requested Features

PHP 8.1 has been officially released and brings numerous innovations, such as enumerations, fibers and performance improvements.

PHP is a server side scripting language that is used to develop web applications. According to W3Techs’ data, PHP is used by 78.2% of all websites with a known server-side programming language. In other words, almost 8 out of every 10 websites that you visit on the Internet are using PHP in some way.

Related: How to Configure Nginx to Work with PHP via PHP-FPM

The language continues to evolve, by releasing their newest PHP 8.1 update, just a couple of days after the formation of a PHP Foundation was announced. This new version, released November 25, 2021, contains many new features, including enums, readonly properties, first-class callable syntax, fibers, intersection types, performance improvements and more.

Let’s take a quick look at some of the new features, performance tweaks and other reasons why you should migrate to PHP 8.1.

PHP 8.1 Highlights

With every new PHP release we can usually expect a small performance increase, and this one seems to be no exception. This newest version also provides capabilities that help developers write more concise code.

Enums (Enumerations)

PHP 8.1 introduces a new data type called Enums. It’s basically a set of predefined values declared in one entity. Enums are an important tool in application modeling as it lets you define your own micro universe of allowed states.

Now you’ve probably used Enums one way or another in your code, but now PHP 8.1 will support enums natively. And this is great news for everyone. In their most basic form, Enums look something like this:

enum Animal {
        case Dogs;
        case Cats;
        case Birds;
        case Reptiles;

Similar to other languages, this is an enumeration type that allows predefined values. The example above creates a new enumeration type called Animal, which takes the following four values: Animal::Dogs, Animal::Cats, Animal::Birds and Animal::Reptiles. Variables can be assigned to these values.

The advantage of Enums is that they represent a collection of constant values, but most importantly, these values ​​can be typed.


Until now, PHP was only intended for writing synchronous code. PHP 8.1 ships with an exciting new feature for writing asynchronous code build right into the language, Fibers.

The introduction of Fibers is intended to remove the distinction between asynchronous and synchronous functions.

You can use Fibers to develop full-stack, interruptible PHP functions, which you can then use to implement cooperative multitasking in PHP.

Fibers pause the entire call stack, so the direct caller of the function does not need to change how it invokes the function. Execution may be interrupted anywhere in the call stack using Fiber::suspend().

$fiber = new Fiber(function (): void {
    $value = Fiber::suspend('fiber');
    echo "Value used to resume fiber: ", $value, "\n";
$value = $fiber->start();
echo "Value from fiber suspending: ", $value, "\n";
Value from fiber suspending: fiber
Value used to resume fiber: test

First-Class Callables

First-Class callables are a new way of referencing Closures and Functions. While previously you could already reference them by using their name as a string or by creating a callable array, there is now this proper syntax to create a callable from any given function, so that you can call it later.

$fn = Closure::fromCallable('strlen');
$fn = strlen(...);
$fn = Closure::fromCallable([$this, 'method']);
$fn = $this->method(...)
$fn = Closure::fromCallable([Foo::class, 'method']);
$fn = Foo::method(...);

In this example, each pair of expressions is equivalent. The strlen(...) syntax creates a Closure that refers to the strlen() function, and so on.

Other Improvements in PHP 8.1

Array and packing now supports string keys as well class constants can be made final so that they can’t be overwritten by child classes. In addition, there’s a new array_is_list() function that determines whether an array only has numerical keys starting from 0 and incrementing by 1, but whether it’s an actual list.

PHP 8.1 introduces a new way of writing octal integers and we also have Pure Intersection types now. This is the opposite of Union Types, which allows any of the declared types. Pure Intersection types are useful if you often use interfaces in your code.

Finally there is the never type. It’s a return type that indicates that a function won’t ever return. That means it will either exit or always throw an exception.

function redirect(string $url): never {
    header('Location: ' . $url);

The goal of the never return type is to indicate and enforce a function that prevents the rest of the called code from being executed.

Last but not least it’s important to mention that the PHP 8.1 image processing and GD extension adds support for AVIF images. An AVIF image offers significant file size reduction compared with JPEG, PNG and WebP and is currently supported on Google Chrome and Mozilla Firefox.

PHP 8.1 Performance Improvements

Each PHP release continues to improve in the department of performance and PHP 8.1 make huge performance improvements for real-life applications.

In fact, the Symfony demo app on PHP 8.1 is ~23% faster than on PHP 7.4, while Laravel runs ~21.5% faster on PHP 8.1 than on PHP 7.4.

PHP 8.1 Performance Benchmark

This is the result of two new technologies implemented in PHP 8.1 and also a lot of small performance fixes and tweaks. So, definitely, it’s good to see how PHP’s performance keeps improving with every new update.

Bobby Borisov
Bobby Borisov

Bobby is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, Bobby has worked as a Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Leave a Reply

Your email address will not be published. Required fields are marked *