PHP 8 will be released in a little over three months, to be exact on November 26, 2020. It's a new major version of one of the most popular languages used in web development. The new version means some breaking changes, but also lots of new features and performance/security improvements. Here are 4 cool new features that I can't wait to use in production.
1. Named arguments
When we pass arguments to a function, we are doing it based on the parameter position. Named arguments allow passing arguments to a function based on the name. This is a good thing because we don't have to remember the argument position anymore and it will also be much easier to skip default values.
// Positional arguments.
$pieces = explode('-', 'piece1-piece2-piece3');
// Named arguments.
$pieces = explode(delimiter: '-', string: 'piece1-piece2-piece3');
// or
$pieces = explode(string: 'piece1-piece2-piece3', delimiter: '-');
The old way of passing arguments will still work, so if this is not something you like you don't have to use it. Some IDEs like PHPStorm already provide hints for argument names, but having this in PHP core is a cool thing if you ask me.
For more information see PHP RFC: Named Arguments
2. Nullsafe operator ?->
I really like this feature. It will cut the number of lines of code that I have to write especially when accessing some deeply nested fields in Drupal. It's a common thing to call a method on the result of an expression if it's not NULL. This usually leads to a deeply nested code like this:
$street_number = null;
$current_user = \Drupal::currentUser();
if ($current_user !== NULL) {
$account = $current_user->getAccount();
if ($account !== NULL) {
$address = $account->getAddress();
if ($address !== NULL) {
$street_number = $address->getStreetNumber();
}
}
}
That is a lot of lines of code just to get the street number. With the Nullsafe operator we can write this in just ONE line:
$street_number = \Drupal::currentUser()?->getAccount()?->getAddress()?->getStreetNumber();
For more information see PHP RFC: Nullsafe operator
3. String contains, string starts with and string ends with functions
Finally, I don't have to use strpos() function anymore to check if a string contains another string. I waited for this function for a very long time.
if (strpos('DS9 centers on the formerly Cardassian', 'formerly') !== false) {}
// vs.
if (str_contains('DS9 centers on the formerly Cardassian', 'formerly')) {}
Checking if a string starts or ends with another string is also a welcome addition.
str_starts_with('Bajoran', 'Baj'); // returns TRUE
str_ends_with('Bajoran', 'oran'); // returns TRUE
For more information see PHP RFC: str_contains
For more information see PHP RFC: Add str_starts_with() and str_ends_with() functions
4. Constructor property promotion
This sounds complicated, but it isn't. This feature will eliminate boilerplate and that is a cool thing. Constructor property promotion is combining class properties and the class constructor. This feature already exists in Hack -- language created by Facebook that is considered as a dialect of PHP. Let's see how this works. Currently, in PHP 7 we have to do this:
class Human {
public float $height;
public float $weight;
public integer $years;
public function __construct(
float $height = 0.0,
float $weight = 0.0,
integer $years = 0,
) {
$this->height = $height;
$this->weight = $weight;
$this->years = $years;
}
}
And the new shorthand syntax in PHP 8:
class Human {
public function __construct(
public float $height = 0.0,
public float $weight = 0.0,
public integer $years = 0,
) {}
}
For more information see PHP RFC: Constructor Property Promotion