What’s new in PHP 8.0?

Tram Ho

I: Introduction.

Recently, php version 8.0 has been officially released, according to experts, php has quite a lot of breakthrough technology to increase the linkage and improve processing speed. So, together we find out what’s new in php8.0.

I: New features.

1: Union types.

Given PHP’s dynamically typed nature, there are many cases where conjugation can be useful. Simply put, we can set multiple data types for the input and output of the function.

public function foo(Foo|Bar $input): int|float;

But Note that void can never be a union type, as it indicates “no return value at all”. Furthermore, the nullable merger can be written using |null or using ? Existing symbols:

2: JIT

The JIT compiler – in time – promises a significant performance improvement, although not always in the context of web requests. I’ve done benchmarks on real-world web applications and it doesn’t seem like the JIT makes much of a difference, if any, on those types of PHP projects.

You can learn more about JIT here .

3: The nullsafe operator

If you are familiar with the null binding operator , you are familiar with its shortcomings: it doesn’t work on method calls. Instead, you need to mediate check or rely on the optional helper provided by some framework:

With the addition of the nullsafe operator, we can now have null binding on methods!

$dateAsString = $booking->getStartDate()?->asDateTimeString();

You can read all about the nullsafe operator here .

4: Assign a parameter value.

Named arguments allow you to pass values ​​into a function, by specifying the name of the value, so you don’t have to consider their order, and you can also ignore optional parameters!

5: Attributes

Attributes, often called annotations in other languages, provide a way to add meta data to classes without having to parse docblocks.

For a quick look, here is an example of what the properties look like, from RFC:

Note that this base Attribute was called PhpAttributet in the original RFC, but was later changed by another RFC.

6: Match expression

You could call it the big brother of the expression switch : match can return a value, does not require a break statement, can match a condition, use a strict type comparison, and doesn’t do any coercion. any style.

It looks like this:

7: Constructor property promotion

This RFC adds syntax lines to create value or data objects. Instead of specifying the properties of the class and a constructor for them, PHP can now combine them into one.

Instead of doing this:

Now you can do the following:

8: New static return type

While it was possible to return self , static not a valid return type until PHP 8. Given PHP’s dynamically imported nature, this is a feature that will be useful for many developers.

9: New mixed type

Some people may call it a necessary crime: mixed types cause many people to have mixed feelings. However, there is a very good argument for solving this problem: a missing type can make a lot of sense in PHP:

  • A function returns nothing or null
  • We are expecting one of many categories
  • We are expecting a suggested non-type in PHP

Because of the above reasons, it is a good thing that mixed types are added. mixed itself means one of the following:

  • array
  • bool
  • callable
  • int
  • float
  • null
  • object
  • resource
  • string

Note that it’s mixed can also be used as a parameter or property type, not just as a return type.

Also note that since mixed already includes null, null is not allowed to make it null. The following will cause the error:

10: Throw expression

This RFC changes throw from a statement to an expression, which makes it possible to throw exceptions in many new places:

11: Non-capturing catches

With old versions we write catch like this:

With version 8.0 we just need to write like this:

III: Summary. Above I mentioned 11 changes I think we often use. There are some more changes I will update in the following article. thanks!

IV: Reference.


Share the news now

Source : Viblo