5 Things You Should Look Forward About PHP 8

PHP is a relatively popular programming language that has been used for developing static or dynamic websites, as well as web applications. The acronym initially stood for Personal Home Page, but the meaning has since been changed to PHP: Hypertext Preprocessor.

One of the primary advantages of this open-source, general-purpose programming language is that it can be embedded into Hypertext Markup Language (HTML), which is also a widely used code. Most web developers use HTML to manage the front-end of their sites while employing PHP for the back-end to handle how the domain runs on the server.

Now, on its 25th birthday, the language will have an update later this year. It may be time to learn new tech skills, especially if you’re stuck at home due to the coronavirus pandemic, and experiment on the new features that PHP 8 would bring.

Here are a few things you should look forward to about the upcoming PHP update:


1. JIT Compiler

The Just-In-Time (JIT) compiler is one of the most-awaited upgrades by web developers. This could be because it has the potential to speed up the language’s execution time. According to Daniel Cooper of Lolly Co, a firm specializing in PHP development, the JIT compiler would make PHP a direct competitor to offline languages, such as python, in terms of power.

The inclusion of the JIT compiler in this update entails the objective of increasing the language’s performance and usability. Since PHP is an interpreted language, its codes still have to be translated into a language that the CPU and other machines understand at runtime.

With this new feature, you can expect a quicker translation process because a compilation of the code would be executed at runtime rather than having the machine interpret everything again. Think of it as running a cached version of the code that’s already interpreted.

The creators of the language hope that JIT would lead to more opportunities for PHP to be used, especially for non-web, CPU-intensive developments. They are confident that the performance benefits would be substantial and that people are missing out because they haven’t been considering the language for the task today.

2. OPcache Extension

The OPcache extension is another update in PHP 8 that aims to boost the language’s performance. If the JIT compiles the translated code, the OPcache extension saves the precompiled script bytecode in shared memory.

It eliminates the need for the system to load and parse scripts every time there’s a request. This feature stores the cached version prepared by the JIT compiler, so to speak.

A typical PHP execution process entails four steps:

Lexing - Also known as tokenizing, this section involves turning a string of PHP source code into tokens, which serve as identifiers for a specific value.

Parsing - Next, the language takes the input, which is the tokens from the first step, and confirms the validity of the token order. It also generates the abstract syntax tree (AST), which is an overview of the source code and will be used in the next stage.

Compilation - Compilation entails the recursive traversing of the AST. It also involves some optimizations, such as resolving function calls through literal arguments, as well as folding constant mathematical formulas.

Execution - Lastly, the system interprets the codes and displays the output according to what the PHP commands say.

If you enable the OPcache extension, your PHP interpreter only has to go through the entire process once. Afterward, the compiled version is stored in shared memory and can be executed upon request right away, bypassing the first three stages.

This feature, however, does more than caching opcodes. It also optimizes your codes as it passes over the script.

3. Union Types 2.0

There’s also an update for union types, which allow developers to state a variable in multiple types. In previous versions, union types were only specified in phpdoc annotations.

This time, the language will provide support for union types in function signatures. With this, developers won’t have to depend on inline documentation any longer. Instead, this function can be defined using syntax.

Some of the primary benefits of supporting union types include:
  • Proper enforcement of types
  • Catching mistakes early in the development process
  • Decreasing chances of displaying outdated information or overlooking edge-cases
  • Checking union types during inheritance and implementing the Liskov Substitution Principle
  • More flexibility through syntax rather than phpdoc

4. Constructor Property Promotion

Speaking of syntax, PHP 8 will also have the Constructor Property Promotion feature, which will provide developers with an updated and more concise syntax. The primary objective for this update is to make the process of property declaration more straightforward and less redundant.

With the current version, all properties must be repeated multiple times before it can be used with objects. It must be displayed in the property declaration, constructor parameters, and property assignment. The process isn’t valuable, especially for classes with several properties and more descriptive titles.

The Constructor Property Promotion update would provide developers with a shorter way to promote properties. It’s also more readable and lowers the chances for errors since it reduces how much boilerplate code developers have to use for value objects.

5. Attributes v2

Attributes, which are also called annotations, are structured metadata forms that can be utilized to verify properties for various objects, elements, and files. Currently, the only way to tag attributes and declare classes or functions is through doc-comments.

PHP 8 will roll out attributes that can be added to code declarations, such as verifying classes, properties, functions, methods, parameters, and constants. These tags are placed before the declarations. They can also be included before or after a doc-block comment.

Conclusion

A lot of developers are holding their breaths for the release of PHP 8 because of its promise to improve the language’s performance and usability. The most anticipated feature seems to be the JIT compiler, which collates a cached version of the code to make the translation and execution process shorter. Other much-awaited functions include the OPcache extension, Union Types 2.0, Constructor Property Promotion, and more support for attributes.

Featured photos: Adobe Stock.
Previous Post Next Post