PHP8 released!

Tram Ho

PHP 8 has been mentioned quite a lot lately (at least for me), and after 5 release candidate, it’s finally ready to be used today – 11/26/2020, instead of bundled Please DO NOT use this version in production, it is an early test version. warning line Please DO NOT use this version in production, it is an early test version.

As a major update, PHP 8 comes with a lot of new features and performance improvements. However, if you are using php7.2 or below, chances are your project will need a little change to run smoothly on PHP 8, if you plan to upgrade: 3 but probably not many people. Where are the environment changes for the project running

New feature

Let’s take a look at some of the cool new features of PHP 8, that I think you will enjoy PHP

Union types

Makes PHP’s data type more flexible. Union types are a collection of 2 or more data types for the same variable

Be a little careful that void cannot be part of the union types declaration, since it returns nothing =]]. In addition, nullable can be used in unions type by syntax |null , or using ?

Nullsafe operator

Everyone’s familiar with the null coalescing operator , of type $a ?? 'baro' is well known, this toy doesn’t work with functions. So you still have to check with rice via if else, or if you use laravel, there is an optional() helper optional() . Eg:

Then with PHP 8, you can summarize it like this:

Named arguments

This toy allows you to pass a value to a parameter, but through the key for that parameter. So you no longer have to care about the parameter order, as well as omit some optional parameters

Match expression

Sibling relative to switch case , match returns the value directly and omits break

Constructor property promotion

Anyone using php must be familiar with this scene

And the next toy will save quite a bit of code just to declare the propertise as above, especially whoever is using Laravel Livewire will love it

Throw expression

With this version, throw will be an expression instead of statement as before. Ie you can write like this

And much more …

This version has a lot of changes, I temporarily bait some interesting things here for you to care about

See more details at the PHP 8 homepage

Share the news now

Source : Viblo