It’s already december 2021 and that means PHP 8.1 has been released. It is full of new features, speed improvements and more. Let’s dive in.
Let’s first start with my personal most exciting and highly requested feature. Enums! With enums we can clean up a lot of code in a lot of PHP applications. Enums can be used to abstract a small set of possible values. For example the status of an order. It can either be “completed”, “cancelled” or “in progress”. With an enum we can make sure that the value is always one of the three.
PHP 8.1 supports two kinds of enums. Pure enums and backed enums. Pure enums are enums with only a value. Backed enums have a key value structure. It’s important to know that you can’t mix the two types. Also all types of the enum values need to be the same, you can’t mix them up.
Pure interception types
PHP already supports union types. This basically means that a given parameter needs to match one of the given types. We can use the pipe character to define a union type for a function. With this new release it’s now possible to have a parameter where the given parameter need to match all the given types. We can use the ampersand character to define that.
This is especially useful if you work with a lot of interfaces and want to make sure that a given parameter is using both interfaces so you know you can call all the correct methods.
The name says it all! Properties that can only be read. It’s a really nice addition for PHP to build up immutable objects that you can pass around. When an object is constructed, it won’t change anymore. This gives a level of certainty and peace when writing code. With this we can make sure that objects actually never change.
Never return type
A cool new return type that has been added is the “never” type. This basically means that a function doesn’t return anything. The never type is useful when a function must throw an exception or when for example “exit” is called inside a function. Libraries like TypeScript also have these return types.
Do note that this is different from “void”. With “void” the flow is still continuous, it just doesn’t return anything. With “never” we actually stop the execution until it’s caught or if it’s the end of the application.
Static analysis tools that help us find bugs in our code and incorrect return types really like this new return type. Without running the code we can already see if the correct type is returned or with the never return type that nothing was returned.
PHP 8.1 also comes with a really nice performance boost. This is mostly because of a feature called “Inheritance cache”. This makes it possible to cache the link between classes which makes it faster to call the same class multiple times. Although we don’t see much of these features when working on our PHP application, it’s still nice to see that improvements are being made in all PHP updates.
Below you can see an overview of the new speed comparison. These results are based on a Symfony demo app. Below you see the average request time per PHP version. Less is better in this case.
There is more in PHP 8.1
Next to all the above features, PHP has had some clean up as well. Some methods are not available anymore or are marked as deprecated. If you’re already working with PHP 7 or PHP 8 there is no need to worry here. You probably won’t be using any of these old methods.
You can find all the changes and examples on the php.net website:
PHP 8.1 brings support for enums which was a highly requested feature. Also the performance of the language got a boost which is always a nice benefit. The typing system has been upgraded to support for features like readonly, intersection types and the never return type. Time to update now and enjoy all the new features and improvements.