This is a great feature to improve memory efficiency and may event prevent memory leaks.
Changes
Furthermore, there are some interesting changes to existing functions that might influence the working of your code, in case you want to switch from an older version to PHP 8. Here is a list of the most important changes.
Stricter and consistent type checks
For user-defined functions, passing a parameter of illegal type results in a TypeError. For internal functions, the behavior depends on multiple factors, but the default is to throw a warning and return null. This Request for Change (RFC) proposes to consistently generate TypeError exceptions for all invalid parameter types, regardless of whether the function is user-defined or extension-defined.
Reference: https://wiki.php.net/rfc/consistent_type_errors
Stable sorting
If multiple elements in the input array compare equal, they will always be sorted adjacently. However, if the sort is unstable, their relative order is not guaranteed and will appear to be random. A stable sort guarantees that equal elements will retain the order they had in the original array.
Reference: https://wiki.php.net/rfc/stable_sorting
Nested ternaries now require explicit parentheses
Deprecate and remove left-associativity for the ternary operator and require explicit use of parentheses instead.
Reference: https://wiki.php.net/rfc/ternary_associativity
Negative array keys added
In the current version of PHP, the behavior of the array_fill function is quite unexpected when you create an array with a negative index. In this case, the first index will be the negative value, after which the array starts counting from 0. This is solved in PHP 8, allowing you to set a negative index for an array, after which the array starts counting from the value, as you would expect.
Reference: https://wiki.php.net/rfc/negative_array_index
Allow a trailing comma in parameter and closure use list
In multiline parameter lists or closure use lists the last parameter was not allowed to have a trailing comma, which in my opinion was very inconsistent and even annoying sometimes. Luckily this will be possible in the near future.
https://wiki.php.net/rfc/trailing_comma_in_closure_use_list
Performance improvements with the PHP 8 JIT compiler
The new version of PHP comes with a Just In Time (JIT) compiler, promising to significantly increase performance. PHP JIT is implemented as an almost independent part of the already existing OPcache. But implementing the JIT Compiler does not necessarily mean better performance. That depends on the use case. To determine for which cases it is the most useful, it is good to know a little more about the inner workings.
With the JIT compiler, code will be compiled at runtime. The compiler monitors the code and identifies commonly used parts as “warm” or “hot”, depending on the usage. Hot parts are compiled as optimized code and cached, so these can be quickly reused instead of compiling the separate parts on the fly. This saves a lot of processing time, so in potential it can hugely increase the performance.
However, it should be noted that the improvement is currently limited for web requests, as the current JIT compiler is mostly optimizing fractal computations. The average web application doesn’t use many fraction computations, making the increased performance very limited. That being said, the JIT compiler could be improved in upcoming minor releases of PHP, increasing the performance for web applications.
Here’s what the RFC says about performance:
JIT makes bench.php more than two times faster: 0.140 sec vs 0.320 sec. It is expected to make most CPU-intensive workloads run significantly faster.
According to Nikita, PHP-Parser became ~1.3 times faster with JIT. Amphp hello-world.php got just 5% speedup.
However, like the previous attempts - it currently doesn't seem to significantly improve real-life apps like WordPress (with opcache.jit=1235 326 req/sec vs 315 req/sec).
It's planned to provide additional effort, improving JIT for real-life apps, using profiling and speculative optimizations.
For now, the new JIT compiler opens up PHP for new areas. If your application does use these computations, the performance is significantly improved. This increased the potential for PHP as a programming language when complex computations are required, like in data science applications and machine learning.
Breaking Changes
Because this version is a major release it brings breaking changes.
We don't expect upgrading will be a big problem for applications which don’t deviate a lot from the current standards.
All of the breaking changes can be found in the upgrade section of the change log.
Conclusion of PHP 8
First of all, this release will bring a lot more consistency to the language, which will redress a big problem that the language had in the past. Along with improved type checking and some long-awaited features will this be a great foundation for the feature of the language.
While the new Just In Time compiler doesn’t significantly increase the performance of web applications (for now), it opens up PHP for different use cases that heavily rely on calculations, such as data science applications and machine learning.
Should I upgrade to PHP 8?
If you are planning a lot of development the new changes could be very helpful. Your code could be made more future proof and maintainable. And if your application is relying on complex calculations, the performance might hugely increase. However, this is unlikely for most existing web applications.
We expect that it’s worth upgrading when you will be actively developing or maintaining existing software in the future. For new applications, we recommend using PHP 8 from the start as the new features and changes allow for more efficient and fun programming.
References