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