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;
}
Code language: JavaScript (javascript)
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');
Code language: PHP (php)
Value from fiber suspending: fiber
Value used to resume fiber: test
Code language: JavaScript (javascript)
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(...);
Code language: PHP (php)
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();
}
Code language: PHP (php)
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.
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.