The PHP (Hypertext Preprocessor) is the programming language that forms the basis of many common content management systems of the web (WordPress, Drupal, Joomla, Magento, …), as well as various frameworks for creating web applications (Laminas, Laravel, Symfony, CodeIgniter…).
After 26 years of life (the first version of PHP was written by Rasmus Lerdorf, Danish-Canadian programmer, in the distant 1994!), PHP continues to renew itself and be one of the indispensable standards in web development.
The most recent upgrade of PHP is currently in development and will be officially distributed on 25 November 2021, the date scheduled for the release of the version General Availability (GA), i.e., that considered stable, following a long series of preliminary versions (alpha, beta, and Release Candidate), as we can read in the PHP Wiki Todo (“things to do”) page.
Table of contents
JIT (Just In Time Compiler)
Va bene, we have a bit “barato”. The JIT has been introduced in PHP version 8.0, and actually in 8.1 there will be at most minor bugfixes (corrections).
Talking about the PHP 8.x series but not mentioning what has been the greatest revolution of this language in recent years would be practically impossible. What are we talking about?
When we say JIT, we refer to the Just-in-time (JIT) compiler.
PHP JIT is implemented as a nearly independent part of the shared memory OPcache (on which we will return in a few lines). It can be enabled or disabled at compile-time of the programs (scripts) in PHP and also at execution time.
When enabled, the source code of PHP files is stored in an additional region of OPcache. So, how did we get to JIT and what is the difference between JIT and OPcache?
First of all, let’s keep in mind that the execution of PHP is a process in 4 phases:
- Lexing/Tokenization: first, the interpreter reads the PHP code and creates a set of derived objects called tokens.
- Parsing: the interpreter verifies if the script matches the language syntax rules and uses tokens to build a Abstract Syntax Tree (AST), a hierarchical representation of the source code structure.
- Compilation: the interpreter traverses the tree and translates the nodes (i.e., the leaves of the AST hierarchy) into low-level opcode codes, that is, numerical identifiers that determine the type of instruction.
- Interpretation: the opcodes are interpreted and executed on the Zend Virtual Machine (VM).
Now, the opcodes to which we have referred are low-level intermediate representations, but they still need to be compiled into machine code, understandable by the processor (CPU). The JIT does not introduce any additional IR module (Intermediate Representation), but rather uses the DynASM (Dynamic Assembler for code generation engines) to generate source code directly from PHP opcodes.
In practice, what the JIT does is bypass the compilation, allowing code to be executed directly from the CPU and without having to go through the Zend Virtual Machine, bringing notable improvements to performance and memory usage as Zeev Surasky, co-author of the original proposal, has argued.
At the moment, the benefits for “user apps” like WordPress will be limited, but the true final goal of JIT is to increase the potential usage of PHP 8.1: bringing it to some low-level computation capabilities that bring it closer to languages like C.
This last one – despite being extremely more complex to use and with its 49 years on the shoulders! – remains almost a mandatory choice when computational speed is the project’s focal point.
Novelties in PHP 8.1: Fibers
The fibers, also known as green threads, are a low-level mechanism for managing the operation parallelism. For most of PHP’s history, programmers have written synchronous code: the execution of functions (also called procedures) is halted until a result is available to return, even for I/O operations (Input / Output), which can be quite slow.
More recently, there have been more projects that have allowed writing asynchronous PHP code to enable simultaneous I/O operations, i.e., where execution can continue without having to wait for a result from a previous “step”.
The fibers are one of these projects, and although they may not be used directly in “user-facing” projects, they will still be adopted by specific libraries like Guzzle, ReactPHP and Amp, which are actually used by developers.
Novelties PHP 8.1: Inheritance Cache
The PHP cache (opcache) – which allows for some files to be available in a “fast memory” so they do not have to be re-read from scratch on every code execution – has been improved by Dmitry Stogov, one of the main core developers working on PHP, with an inheritance caching mechanism.
Thanks to its new creation, when in our programs we will declare classes (which are one of the fundamental concepts of so-called object-oriented programming) and these classes “inherit” characteristics from other classes as well (parent-child relationship), the existence of this link will be “memorable”. This change adds to the already introduced possibility of pre-loading classes in PHP 7.4.
The final outcome is the same we expect also from fibers: faster websites and applications for end-users.
Try Hosting4Agency on a site of your choice for 30 days and compare the performance before/after the trial period!
New PHP 8.1 Functions: Intersection Types
Now, for our more “restless” readers, given that we are talking about a programming language, let’s get into the detail of a modification that directly concerns the way of writing code, introduced among the novelties of version 8.1: support for intersection types.
To better understand its usage, let’s first keep in mind that it is possible to add type declarations to the arguments of our functions, i.e., to those variable parameters that “we feed” to our code, as well as to the values these procedures return.
This assignment, also called type hinting (“suggestion of type”), guarantees that the value is of the correct type (data / text string / integer number / decimal number / class or type interface “who knows”) at the time of the call.
If however this requirement is not met and we find ourselves for example with text when we expect a date, thanks to type hinting an error of the category TypeError is generated immediately. In turn, this helps us better debug the code, that is, to identify lurking errors that could come back to bite us in the long term.
However, declaring a single type has its limitations, and the union types (introduced in PHP 8.0) help us overcome this issue by allowing us to declare a value with multiple types: the input will need to satisfy at least one of them.
Now in PHP 8.1, we will also have intersection types that require satisfying more than one requirement. The classic case is that of classes implementing multiple interfaces.
For a very generic example, in our software there might be a class “Asus”, which implements (i.e., actually realizes) not just one but two different abstract models (the so-called interfaces), namely “Telefono”, where the fundamental functionality is that of calling, and “OggettoCool”, which instead will have to have a completely different nature, with its own operating system and everything we expect from a modern smartphone.
PHP 8.1: How can we assist you
Sappiamo quanto sia importante specialmente nel nostro settore essere sempre all’avanguardia sotto ogni aspetto, per cui supportiamo già il PHP 8.0, e il 25 novembre 2021 non ci faremo di certo trovare impreparati per la versione PHP 8.1!
Anzi, saremo tra i primi ad averla a disposizione, pronta all’uso, per tutte le agenzie a cui forniamo servizi e che la desiderano.
The infrastructure of Hosting 4 Agency – concerning PHP – allows us to coexist on the same server any version of PHP we wish to have and assign it to a whole resource zone, which corresponds to a folder with just a few clicks.
Any site or application underneath that point of the filesystem will “run” (as we say in jargon) with the desired PHP.
If you manage one or more websites and want to be sure they are always protected, fast and updated promptly to the most innovative solutions, we can take care of it for you, lifting any type of worry and time loss off your shoulders. To learn more, take a look at our packages!