Should you update to PHP 8?

Yes, you definitely should update to PHP 8, if you are using an older version. There will be many new features, regardless of the version you are coming from. What should you expect?

When was PHP 8 introduced? 

PHP 8 (version 8.0.0) arrived on the 26th of November, 2020. What you should really check about it is the JIT, Named Arguments, Union Types, Match Expressions, Constructor Property Promotion, Nullsafe Operator, Error Handling, and more.

Since then, there have been a few minor upgrades, mainly focusing on stability and bug fixing:

PHP 8.0.1 – 07.01.2021

PHP 8.0.2 – 04.02.2021

PHP 8.0.3 – 04.03.2021

PHP 8.0.5 – 29.04.2021

PHP 8.0.6 – 06.05.2021

PHP 8.0.7 – 03.06.2021

PHP 8.0.8 – 01.07.2021

PHP 5 to PHP 7.2, should you update?

Yes, you should update if you are using any version previous to PHP 7.3. We do not even mention all the new features and bugs fixed that you will get with the new version. The main reason is that the support for all previous versions up to PHP 7.3 is over. They are outdated, and you must update them for better security and compatibility.

Should you update from PHP 7.3 or 7.4 to PHP 8?

The answer is yes. The official support of PHP 7.3 will finish on 06.12.2021, which is getting closer, and those of you who are using 7.4 should consider updating for the new features. Still, if you are on version 7.4, you can use it without problems until 28.11.2022 and get support. By then, PHP 8 will be in its PHP 8.1 version (expected on 25.11.2021), and you can get an even safer and stable version.

What will you get with the new version?

These are the reasons that you should update to the new version 8:

JIT (Just-in-time) compiler. “PHP JIT is implemented as an almost independent part of OPcache. It may be enabled/disabled at PHP compile time and at run-time. When enabled, native code of PHP files is stored in an additional region of the OPcache shared memory and op_array→opcodes[].handler(s) keep pointers to the entry points of JIT-ed code.”

Thanks to the Opcache, the need to load and parse scripts per request is lowered, and that way, you save time and CPU usage.

Constructor Property Promotion – a simplified method for declaring properties. It will lower the repetition of properties and makes it easier to use with objects. A simpler syntax is better for the coders.

Named Arguments – they pass an argument to a function based on the parameter’s name, not its position. Add arguments to a function just by adding the parameter’s name before the value.

Match Expressions – it is similar to the “switch”, but it allows return values, and the new match expression makes identity check comparison (===) instead of a loose comparison (==).

Union Types – it is a useful feature that can unite 2 or more types and show that any of them can be used. In the previous version, the way was to specify union types was only in phpdoc annotations.

Nullsafe operator – with the nullsafe operator, we can finally have behavior like the one of the coalescing operator, on method calls. Use the previously known coalescing operator with array keys and the new nullsafe operator with method calls.

Attributes. They are used to specify different properties for objects, elements, and files in metadata (annotations). Before, you could add metadata only by adding doc-comments.

How to migrate?

You can find a guide on the PHP site, depending on the version you are using. Follow it and see what’s new you will get and if there might be any incompatibilities.

Leave a Reply

Your email address will not be published. Required fields are marked *