PHP 8 fresh exciting features — part 1

Amirhossein Shahbazi
2 min readOct 24, 2020

--

So the big blue elephant is back once again, and not empty-handed. Mark your calendars for November 26! But before that, let’s review some of the most exciting features introduced in PHP 8. (BTW this is my first ever blog post)

The RC2 version of PHP got out a few days ago. feel free to test the new features I’m gonna talk about yourself. you can download it from here.

Trailing commas

This might not come up as a top-notch feature at first, but how many times were you working with PHP 7.X and made a wish for a trailing comma at the end of the parameters list?

// not possible in the earlier versions of PHP
$trailingCommas = function (
$a,
$b,
$c,
) {
// something happens here...
};

Named arguments

Speaking of parameters and arguments, how about using named arguments in your code?

Implementing named arguments in PHP 8 surely helps to prevent a lot of crowded documentation.

// not possible in the earlier versions of PHP
function joinNames(string $first, string $second) : string
{
return $first . " " . $second;
}

echo joinNames(first: "Amir", second: "Shahbazi");

str_contains method

This method takes two arguments as haystack and needle, searches for the needle in the haystack, and returns a boolean value (true or false) based on the search result.

$haystack = "one ring to rule them all";
$needle = "ring";
echo str_contains(haystack: $haystack, needle: $needle);
// returns true

Union types

Union types let you use multiple types of values rather than one. You can use union types in the form of T1|T2|T3|

class Staff
{
public int|float|string $id;
}

$amir = new Staff();
$amir->id = "9251216";
$amir->id = 9251216;

JIT

Last but not least, JIT has been finally introduced into PHP 8. It’s expected to be a significant performance improvement. As PHP wiki says:

using JIT may open the door for PHP being more frequently used in other, non-Web, CPU-intensive scenarios — where the performance benefits will actually be very substantial — and for which PHP is probably not even being considered today.

It is debatable whether implementing JIT into PHP 8 will really improve performance or not.

There’s a benchmark that you can look into.

Part 2 coming soon. you can see some of the implemented features yourself.

--

--

No responses yet