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 ten websites you visit on the Internet use PHP in some way.

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

The language continues to evolve by releasing its newest PHP 8.1 update just a couple of days after the project announced the formation of a PHP Foundation. This new version, released on 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 you should migrate to PHP 8.1.

PHP 8.1 Highlights

With every new PHP release, we expect a small performance increase, and this one is no exception. However, this latest 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 a set of predefined values declared in one entity. Enums are essential in application modeling as they let you define your own universe of allowed states.

You’ve probably used Enums one way or another in your code, but now PHP 8.1 will natively support them. And this is excellent 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.

Fibers

Until now, PHP was only intended for writing synchronous code. PHP 8.1 ships with an exciting new feature for writing asynchronous code built 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";
 
$fiber->resume('test');
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 support 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 one 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 declared types. Pure Intersection types are useful if you often use interfaces in your code.

Finally, there is the never type. A return type 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);
    exit();
}

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 add 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 performance department, and PHP 8.1 make substantial performance improvements for real-life applications.

For example, 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 minor performance fixes and tweaks. So, it’s good to see how PHP’s performance keeps improving with every update.

Bobby Borisov

Bobby Borisov

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

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%

Leave a Reply

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