PHP is one of the easiest open source programming languages to learn. Similarly, PHP is very popular and suitable for website development and can also be included in HTML. The meaning of open source is free to use for all programmers who wish to use it. The current version PHP 7, incorporated a great optimization in speed, by consequently increased its prestige. On the other hand, the latest version PHP 8.0 brings a major update.
On the other hand, months back after a period of uncertainty, he announced that the launch of PHP 8 was for November 26, 2020. This also coincides with the usual 3-year cycle of PHP.
To view the tutorials that have made PHP, click here.
PHP 8.0: What’s new?
Union Types
Given the dynamic nature of PHP, there are many cases in which union types can be useful. Similarly, union types are a collection of two or more types so we indicate that any one of them is valid. When we specify the value of that argument or the return type of the function.
public function foo(Foo|Bar $input): int|float;
Please note, void
can never be part of the union, because it indicates “no return value at all”. Also, the nullable
union can be written using |null
, or using the notation ?
public function foo(Foo|null $foo): void;
public function bar(?Bar $bar): void;
Attributes
The attributes or annotations allow you to include metadata to the class, something that will seem familiar if you work with frameworks like Symfony or Laravel.
Similarly, according to the proposal in the RFC, which from PHP 8 may specify attributes/annotations. Without the need to resort to commenting blocks.
Also, the first thing they must do is create an attribute as if it were a class. Then use the attribute PhpAttribute
so that it can be identified as such.
Constructor property promotion
The current version of PHP, includes the well-known “syntactic sugars” that allow to simplify the code. That the user writes for shorter versions. Instead of specifying class properties and a constructor for them, PHP can now combine them into one.
Instead of doing this:
class Money
{
public Currency $currency;
public int $amount;
public function __construct(
Currency $currency,
int $amount,
) {
$this->currency = $currency;
$this->amount = $amount;
}
}
You can now do this:
class Money
{
public function __construct(
public Currency $currency,
public int $amount,
) {}
}
Mixed Type
Many programmers have tried to write an argument or the return value of a function using mixed. Thinking it was a valid type, however, they get an error to do so.
- a function that returns
void
ornull
. - an argument that can receive values of several types.
However, with PHP 8.0 they allow you to write using mixed arguments, properties and return values to represent several of these values:
array
bool
callable
int
float
null
object
resource
string
As you can see mixed
also includes null
, so it will not be valid a “tipado” as the following: ?mixed
.
JIT
The JIT compiler which is the acronym for “just in time” which promises significant performance improvements. Although not always in the context of web requests. In other words, JIT is a technique that will compile parts of code at runtime. So the compiled version can be used instead of the interpreted one.
For more information about PHP 8.0, visit the official announcement
Conclusion
The current version, PHP 8.0, incorporates many new features at both the functional and syntactic levels. This is to prove that it is not an “old” language, far from it.
Similarly, with the arrival of the latest version of PHP, many web pages written in older versions will soon no longer be shareable. It is also important to keep the website code updated and use the newer versions of PHP. They can bring some advantages, such as a considerable performance increase and a high level of security.
On the other hand, with the inclusion of JIT in PHP, they will be able to become an all-rounder. And in the same way, cover more space for those who were not reached before because of its performance.