PHP 8 was launched on November 26, 2020. Compared to its predecessor, the latest version of this programming language is more complete, both in terms of features, performance, and error fixes.
What is PHP 8? What are the new features? Well, in this article we will explain it in full. Let's see this article to the end!
What is PHP 8?
PHP 8 is an update of PHP 7.4 which will stop active support in November 2021. Unlike PHP 7.4 which is only a minor update, PHP 8 is a major update of the PHP programming language.
What does a major update mean? So, this latest version offers better performance with a variety of new features. Some of its excellent features are Just In Time Compiler, Weak Maps, and Named Arguments.
PHP 8 Features and Advantages
1. JIT (Just In Time) Compiler
JIT Compiler is a technique to improve the performance of interpreted programs. Because before the program is executed, the JIT Compiler has compiled the program into machine language code.
With the JIT Compiler technique, code execution becomes much faster than previous versions of the PHP interpreter. How is the interpretation process?
In the previous PHP compiler, the interpreter would parse, compile, and execute the code. The process is repeated every time there is a script request. It causes wastage of CPU space.
However, the PHP 8 JIT Compiler has actual machine code that can work with OPCache. So, when there is a request to the script and the cache is available in OPCache, the script will be directly forwarded to the CPU to compile.
In addition, PHP 8's JIT Compiler has two JIT compilation engines, namely functions and tracing. Function-based JIT optimizes code within a single function scope. Meanwhile, tracing-based JIT optimizes all code tracking and search.
When the JIT Compiler feature is enabled, it can increase the performance of PHP based applications by 3-5%. Meanwhile, standard PHP application performance will increase 3 times faster.
2. Weak Maps
Weak Maps are data structures that can hold objects. Unlike Weak Reference in PHP 7.4, Weak Maps can also clear the cache of objects that are no longer referenced.
In PHP, when a variable from an object is deleted, there will be a reference check to that object. If there are no other variables, it means it's safe to delete the object. Now, with the Weak Maps feature in PHP 8, the deletion process becomes more automatic.
3. Union Type
Union type is a value that has the same representation in a position and can accept several types of variables. Well, because PHP supports Union Types, you can declare variables of several types.
- Type or null, using the special syntax ?type
- array or Traversable, using the special type iterable
In earlier versions of PHP, arbitrary binding types were not supported by that language. So, programmers tend to use PHPDoc annotations as a solution.
Thankfully union types are now native to PHP 8. So, you don't need to use PHPDoc annotations anymore. You can specify multiple parameter types with the syntax T1|T2|… in all positions.
There are several advantages of the union type. First, because of the self-assignable parameter value types, you can better avoid possible errors. Second, less updated type information can be avoided.
4. Using ::Class For Objects
In previous versions, you had to use get_class() to assign classes to objects. As of PHP 8 however, you can use ::class to objects to ensure streamlined source code.
5. Attributes
Attributes are a way of annotating additional configuration and information, making it machine-readable in declarations in code.
In previous versions, you documented parameters and values using PHPDoc. Well, the Attributes feature in PHP 8 makes it easy for you to use structured metadata with native PHP syntax.