PHP 8.0 Just In Time with Attributes and much more
Wait a little while and then the new version of PHP comes from version 8.0. This brings a lot of fun new functions such as the Jit Compiler, Union types, and attributes. I will briefly discuss these 3 things so that you get an idea of what the coming version entails.
Time has almost arrived and I am not talking about the birthday of Sinterklaas, no I am talking about the release of the new version of PHP. PHP 8.0 will be released on November 26. This a major release and ensures that there will be a number of important changes, as well as many new functionalities and performance improvements.
Because there are a number of important changes, you may have to update your code to make it run at PHP 8.0. But if you have kept your PHP versions up to date, the upgrade to version 8 should not be too difficult since most Breaking Changes are already outdated for the 7.* versions. And no worries, we list the outdated functions.
A Union type can accept values of several different types instead of 1. In the current version of PHP this is not accepted with the exception of 2 special Union types
- Type or null, using the special syntax ?Type.
- Array of Traversable, using the special iterable type.
- Types are actually enforced, so mistakes can be noticed early.
- Because they are being enforced, the chance is smaller that type information becomes outdated or missing edge cases.
- Types are checked during inheritance, which maintains the Liskov replacement principle.
- Types are available via Reflection.
- The syntax is much less standard than PHPDoc.
The RFC requests whether we can support Union Types in function signs from now on, so that we are no longer dependent on inline documentation, but they can specify with the Syntax T1 | T2 ...
More about Union types
Just-in-time (JIT) Compiler is one of the features that will be set live with PHP 8.0. The Jit Compiler was already in for test purposes, but it is now ready to be used. It should ensure a significant performance increase. PHP is not compiled, but rule interpreted by rule. JIT is a technique that compiles parts of the code during Runtime, so that the compiled version can be used instead. Think of it as a "crepse version" of the interpreted code, which is generated during runtime.
Pedro Escudero has already tested JIT. It has used a simple script to test different versions of PHP Performance. He has run this script 100x and caught the average time. Below you can see the benchmark results:
|Time in seconds
|8.0 (without JIT)
|8.0 (with JIT)
As you can see, the difference between 7.4 and 8.0 without jit is not very significant. But with the Jit Compiler, performance is improved by around 45%.
Attributes are declarative metadata that between "< <" and "> >" to stand. They can be added to other parts to specify properties. Another name for attributes is Annotations. This is a term known from doctrine both are often used interchangeably. PHP has gone specifically for the name attributes to prevent the confusion with doctrine as much as possible.
str_Contains is one of the new functions that has taken a while. This allows you to see if a string is in a different string and you no longer have to use str_pos ().
We have now discussed some of the large features, but there are of course more. Do you want to know what else can be done in PHP 8.0. Then click below
As you can read, version 8.0 brings us a lot of exciting and fun new features that can bring a lot of new fun things. As mentioned it is a major release so there may be things that could break your code. But if you have kept your code up-to-date, it must all be better than expected. In any case, I can't wait to play with PHP 8.0