Recently we set up a PHP boilerplate to use for future projects. This boilerplate allows us to set up an intuitive headless CMS in combination with a high-performance API in a short period of time. This upgrades the workflow and lowers the overall cost. To set up and implement this framework we started using the newest version of PHP, namely PHP 8. It was released in November 2020 but, as we know from experience, there are always some issues that need to be fixed before it’s seen as a stable platform. So at the beginning of May, we gathered with our PHP developers for a mini hackathon to explored the possibilities of the new PHP version and set up a complete and useful framework. Here are some features we are wildly enthusiastic about.
New features we started using in PHP 8
With the introduction of JIT, Just In Time compiler, intensive processes get an enormous boost. By checking at runtime which parts of the bytecode need to be converted to machine code, and by eliminating the parts that will use up too much overhead, JIT promises significant performance on PHP.
JIT compilation was already a big thing in other programmer languages and with the implementation of this compiler in PHP, they are part of the club. This establishes the evolution of PHP towards a steady backend environment.
Overall, JIT ensures better performance as you can see on these benchmarks:
2. Constructor property promotion
To make use of a property in a class, you needed to declare at least 4 times this specific property. This process is updated in the new PHP 8 release and makes the code cleaner and more readable.
Past situation vs current situation:
3. Named arguments
Named arguments are a real plus in day-to-day programming life. Some IDE’s already integrated functionalities that show the order, name, and type of the parameters you are going to address. But since this is outside your IDE, it’s not always easy to look at the code, especially with pull requests. Other than that it is much more logical to integrate it into the programming language itself.
Thanks to named arguments, you can easily see which value belongs to which parameter. It implies that the order of your parameters isn’t any longer important. And therefore this functionality allows developers to write cleaner and more flexible code.
4. Union types
We use union types to direct any number of arbitrary types to a certain property. This is a welcome upgrade because beforehand we could only use union types via doc comments. This is part of the strategy of PHP to integrate as many functionalities as possible, instead of adding them in doc comments.
5. Attributes v2
Actually, this is one of the biggest changes in PHP 8, the Attributes support. Beforehand charing structured meta-data was only possible via PHPdoc comments and it was the task of “userland” a.k.a. the PHP developer community to define the structure of it. From now on this will be included in PHP 8.
Although, you can still combine doc comments with annotations so for backward compatibility this will not be an issue.
To upgrade or not to upgrade?
We feel that some developers are still wondering whether or not to upgrade to PHP 8. Here's what we think. We notice that PHP 7.3 and earlier versions only receive security updates and that version 7.4 will only receive full support until the end of the year. So an upgrade to PHP 8 is not only necessary to ease the life of developers, but it will also be essential to ensure full support for your product in 2022.
Migrating from PHP 7.x to PHP 8 is easy and super fast because the largest part of the breaking changes is phased out in older versions of PHP 7.
PHP 8 is the next logical step forward in the evolution of PHP. Improvements can be implemented without having to compromise on performance and user experience. Other than that PHP has a lot of small and big QoL improvements to improve development and thus our developers’ daily life.
So the question is more like “when will you be upgrading your codebase”?