Right now, PHP is one of the most used programming languages in the web development world. Based on the survey from W3Techs here, PHP is used by 79.2% of all the websites in the world. This means, until today, PHP gets better and better in every aspect.
In November 2020, PHP 8 was released. A lot of new features & functionalities have been added, as well as optimizations and fixing some features that may lead to inconsistencies. In this article, I’ll show you 10 things that PHP 8 has so that you can code better than before. Let’s go.
“PHP is about as exciting as your toothbrush. You use it every day, it does the job, it is a simple tool, so what? Who would want to read about toothbrushes?” — Rasmus Lerdorf (The Creator of PHP)
- Constructor Property Promotion
- Union Types 2.0
- Weak Maps
- Trailing Comma in Parameter List
- Allow ::class syntax on objects
- Named Arguments
- Nullsafe Operator
- str_starts_with() and str_ends_with()
- Match Expression v2
#1 Constructor Property Promotion
In the previous PHP version, when we want to initialize some variables inside the constructor, this is what we usually do:
On lines 9–10, we’re assigning again the variables there. Things will be more complicated if we have a lot of properties to be passed. In PHP 8, we don’t need to do that anymore, instead, we can do something like this:
The code above is more simple, shorter, and readable.
With the latest features of constructor property promotion above, we can easily make nullable properties into the constructor.
Notice that on line 6,
$msg = null is not enough for PHP to make the property nullable by default, instead, it needs a question mark in front of the type
#3 Union Types 2.0
Previously, when we declare a variable that has more than one types, this is what we do:
Those comments mean that the variable can only accept two types, but of course, the compiler doesn’t recognize the comments. Luckily, in PHP 8, we can have a variable that can hold more than 2 types.
Also, we can specify whether the variable can hold a null value or not, with
#4 Weak Map
The weak map is a collection of data, with key-value pairs, in which a key is an object that weakly referenced its value. The special thing about using the weak map is, whenever the object (that is being used as a key) being removed, then the weak map collection will automatically being removed as well.
The use of the Weak Map can prevent a memory leak because the Garbage Collector will automatically remove the collection if the key object is being removed.
#5 Trailing Comma in Parameter List
The trailing comma is not new in PHP, but in PHP 8, trailing commas can be used in the functions, methods, and closures.
There are some good benefits of using the trailing commas, that you can see from this article why you should use the trailing commas.
#6 Allow ::class syntax on objects
This is an additional feature of PHP 8, wherein the previous version of PHP, when we want to get the class name, this is what we usually do.
But in PHP 8, the code below will do the same thing as the get_class() method.
#7 Named Arguments
Imagine you have a lot of properties to be passed in a constructor, what we do in the previous version is similar to this:
$obj = new Object(1, 'Hello');
It might not clear/easily readable on what values belong to what arguments. In PHP 8, we can pass properties with the named arguments.
So it’s more convenient to read, right?
#8 Nullsafe Operator
Imagine you’re creating a method chaining in your app. At some point, one of the methods unexpectedly returning null, and of course it will throw an error. In PHP 8, we can safely check whether the method is returning a null or not, using
On the code above, if we run
$hello->methodA()?->print(); it’ll return null, and won’t throw any error. Because it’ll check whether the methodA() is returning a null or not. It’ll have a different outcome if we run the class above with this command:
$hello->methodA()?->print(); that definitely will throw an error.
#9 str_starts_with() and str_ends_with()
There are some new functions added in PHP 8, and str_starts_with() & str_ends_with() are some of them. These functions check whether a given string starts or ends with another string
str_starts_with(string $haystack, string $needle); // return bool
str_ends_with(string $haystack, string $needle); // return bool
#10 Match Expression v2
Match expression is similar to switch, but it’s more concise and shorter to use.
The only difference between them is that switch compares values loosely (==), while match compares the identity check (===).
That’s a lot of new things & improvements that have been added for PHP 8. Actually, there are a lot more than these lists. The good news is, as a PHP Developer, we definitely know that PHP is getting better and better every time, and hopes it’ll always get the majority of the market.