Here comes the latest and a major PHP update, PHP 8.1. Released on 25th November last year, PHP 8.1 comes with many a major update and new features and prepares itself for the deprecation and restriction of some unwanted functions.
With PHP 8.1, PHP’s type system has been improved further. Additional defensive programming features have also been added. The PHP 8.1 life-cycle is just starting as release managers Joe Watkins, Ben Ramsey, and Patrick Allaert will be continuing to release minor updates till the end of 2024.
Enums, Fibers, never return type, Intersection Types, readonly properties, and first-class callable syntax are some of the highlights of PHP 8.1. However, a few features have been let down, which might complicate upgrading legacy PHP applications to PHP 8.1.
Primary highlights of PHP 8.1
PHP 8.1 includes features such as Enums, one of the most anticipated and requested features. Additionally, the version adds Fibers, making simultaneous PHP applications possible, setting PHP further apart from the traditional request-response pattern. We will now understand what these features are and how to use them.
Enums
PHP 8.1 now supports Enumerations. Enumerations have a fixed number of possible values. For instance, the number of playing pieces in chess is 32, distributed between two colors- Black and White, which is fixed.
In PHP enumerations are declared using Enum.
enum Color { } |
Now, this Colour enum can be used to enforce types when accepting or returning a colour value:
function pick_side(Color $color) {} |
pick_side(Color::Black); pick_side(Color::White); |
Enumerators make the code more readable and reduce the likelihood of unexpected application behavior.
Fibers
Fibers are among the many new features introduced in PHP 8.1. They are essentially code blocks that maintain their own stacks (variables and states) and can be started, suspended, or terminated cooperatively with the main code.
Just like threads in an operating system, Fibers don’t have a certain point at which it pauses or stops. Starting, suspending, and terminating them all can be controlled by the program itself, allowing a smooth flow of the main program execution and Fiber execution.
The Fibers allow the code block to be suspended, and the resultant data is returned to the main program. After the Fiber is suspended, the program can resume from where it left off.
Read-only properties
Read-only class properties are now available in PHP 8.1. One can’t change the properties of a read-only member once it has been initialized.
Read-only class properties are declared using the readonly keyword.
class User { public readonly int $col; public function __construct(int $col) { $this->col = $col; } } |
$col = new User(69); $col->col = 16; // Not allowed as the properties of readonly properties can’t be changed |
Type system Improvements
PHP 8.1 maintains syntax and functionality for writing more defensive and typed programmes. This might also imply that PHP 8.1 will add additional deprecation alerts to legacy programs including mismatched class signatures or, in some cases, a lack of types when extending built-in PHP classes.
Intersection Types
Through Intersection types, one can declare a type for a parameter, property, or return type, which can enforce that the values are a part of all the declared classes and interface types. Since it is not possible to combine Union Types and Intersection Types in the same declaration, PHP 8.1’s implementation of Intersection Types is called “pure” Intersection Types.
To declare intersection types, the class/interface names are preceded by the ‘&’ symbol.
Here’s an example of how PHP’s Iterator and Countable interfaces could be used. Iteration of the class object is possible through the Iterator interface. Nevertheless, such objects cannot be counted unless they implement the Countable interface.
function example(Iterator&\Countable $data)
{ //$data is an object of the class that implements Iterator&\Countable
|
Never return type
The function/method declared as never return type doesn’t return any value, instead it throws an exception or terminates with a die/exit call.
Similar to the existing void return type, the never type ensures that the program terminates or throws. To put it differently, a declared function or method may not even call return; never keyword is used to declare something as never return type.
function redirect(string $number): never { header(‘contact ‘ . $number); exit(); } |
New hashing algorithms
PHP 8.1 now supports xxHash and MurMurHash3 hashing algorithms. Their speed is nearly equal to that of storage media, making them some of the fastest algorithms for hashing data. These two hashing algorithms are the first to be included in PHP’s standard library.