PHP 8 – What is important to know?
The new version of the popular programming language PHP 8, is now released (November 26, 2020), bringing with it many new features and performance improvements. Among the main innovations are the JIT compiler, union types, additional attributes.
One version of PHP has been actively maintained for about two years. When they expire, there is another year or two of security support, and updates are released only in case of critical problems. In the meantime, the next version is usually out. At this time (November 2020), versions 7.2 , 7.3 and 7.4 are actively supported , with version 7.2 addressing only critical security issues, and version 7.3 set to enter the same phase on 6 December 2020. You can always to follow the currently supported versions of the official PHP site .
The news of PHP 8 has been circulating on the web for a long time. Millions of consumers are looking forward to the official release date: December 3, 2020. There are less than a month left until then, so it’s important to know what to expect. Here are some of the highlights.
PHP 8: JIT Compiler
Undoubtedly the most anticipated upgrade by the developers is the JIT compiler. The mechanism, available for testing since PHP 7.4, is now officially part of PHP 8.
The idea for the implementation of JIT appeared in PHP 7, and Dmitry Stogov started this project in the distant 2011. Since then, he and other leading developers have tried three times to implement the technology. The JIT is responsible for compiling the script into machine code immediately before execution. Through it, the application is executed “on the go”, much faster than the previous interpreter.
JIT constantly monitors the code during its execution. He understands which parts are replayed. The most commonly used fragments are compiled and saved as machine code. As a result, the performance of the software is significantly improved. The compiler is implemented as an independent part of OPcache and can be activated or deactivated.
Because PHP is an interpreted language, its codes must be “translated” into a language that the CPU and other machines understand. Therefore, the introduction of JIT will allow PHP to seriously enter the web, for example in high-profile areas such as the development of artificial intelligence, Machine Learning or the Internet of Things.
PHP 8: OPcache extension
The OPcache extension increases language performance. If JIT compiles the translated code, OPcache saves the pre-compiled code in shared memory.
The extension saves the system the need to load and analyze scripts with each request. It stores the cached version prepared by the JIT compiler.
The implementation process in PHP is done in the following four steps:
- Tokenization : The interpreter reads the PHP code and develops a selection of tags.
- Parsing : The interpreter reviews the script and ensures that it matches the tags used to create the abstract syntax tree (AST). It also checks and ensures that the syntax rules match.
- Compilation : AST translates to low level Zend codes. This creates identities that allow the interpreter to identify the Zend VM instruction.
- An interpretation that allows Opcodes to be interpreted so that they can run on a Zend VM.
If you enable OPcache, the PHP interpreter goes through the whole process only once. The compiled version is then stored in shared memory. Upon request, it can be performed immediately and so the first three stages are skipped.
PHP 8: Constructor Property Promotion
This innovation will provide developers with an updated and shorter syntax. It allows the declaration of class property and constructor assignment directly by the constructor. The update will make the process of declaring properties easier and more readable and will reduce the risk of errors.
PHP 8: Attributes 2.0
Attributes or annotations are structured forms of metadata that can be used to define properties of various objects, elements, and files. Currently, the only way to mark attributes and declare classes or functions in PHP is through doc-comments.
Optional new attributes will be added to code declarations: checking classes, functions, constants, methods, parameters. The tags in question shall be placed before the declarations to which they belong. They can be included before or after a doc-block comment.
PHP 8: Union Types 2.0
Union types can define a set of types (two or more) for input and / or output data. Until now, the syntax of the language did not provide such opportunities. An option to work around the problem was to define types in the annotations.
In PHP 8 all Union Types can be written as? or as null.
PHP 8: Named arguments
Named arguments allow you to pass values to a function by specifying the name of the parameters so that you do not have to consider their order. You can also skip the optional ones.
And more
Other innovations worth noting are the Nullsafe operator , which will now work when calling methods, and Match Expression . It is worth mentioning the new possibility to use :: class on objects, instead of the previous version with get_class (). In PHP 8 we will also see a lot of new features like fdiv () , get_debug_type () , get_resource_id () , str_contains () , str_starts_with () and str_ends_with () .
The @ operator no longer mutes fatal errors
It is possible that this change will show errors that have remained hidden so far. If you have not set display_errors = Off on your production servers, you better do so. Many of the errors that have so far triggered only warnings or notifications have now been converted to the appropriate format. A useful list of changes can be found here.
PHP 8: What WordPress Users Need to Know
Should I upgrade right away? No. The upcoming major version of WordPress, 5.6, is intended to be “beta compatible with PHP 8” according to the November 18 WordPress dev chat. This means that most core WordPress functionality will work, but unexpected bugs may still occur for some time, even without the presence of additional plugins or themes. WordPress has called for additional testing with PHP 8 in order to find and fix as many remaining bugs as possible.
A vast number of WordPress plugins and themes will not be immediately compatible with PHP 8. Those that do not run into fatal errors during normal usage may still show unexpected behavior for some time.
The transition to PHP 8 is one of the broadest and most impactful changes the language has ever seen. While it will be worth it in the long run, WordPress site owners and developers may be in for a rough ride in the short term. If you’re a website owner, start keeping a watchful eye on which of your plugins and themes are being updated or tested for compatibility and make a plan to replace the ones that aren’t. If you’re a developer, start testing your code and any dependencies on PHP 8, if you’re not already, and start making a plan to fork or replace any libraries that aren’t being updated. The WordPress ecosystem has been through difficult transitions in the past, and our open-source community has always grown and adapted.
In conclusion
If you’re still hesitant to upgrade to PHP 8, keep the following in mind:
- Newer versions of PHP are usually more secure, have fewer errors, and run scripts faster.
- If a site is created with PHP and the code is compatible with the latest version of the language, it will work the same way or better. If you use WordPress or a similar CMS and update it, you’ll probably notice performance improvements.
- In case your code is outdated and is not supported by the new version of PHP, your site will give errors and may even stop working completely! Therefore, first check compatibility carefully.
Related Articles
If you enjoyed reading this, then please explore our other articles below:
More Articles
If you enjoyed reading this, then please explore our other articles below: