LATEST FEATURES OF PHP 8

Emir Burgazli
5 min readDec 14, 2020

Hello to everyone. Today I want to talk about PHP 8 innovations. As everyone knows, the new version of PHP 8 was released on November 26, 2020. So what kind of features does it have? What changed? Let’s take a look at these now. Although not all the details, I will talk about the features that stand out as much as possible.

JIT (JUST IN TIME)

Many new features and performance improvements are offered with PHP 8. Let’s start with JIT, which attracts the most attention and attention in this version. PHP was a language interpreted at run time. With JIT, it begins to be interpreted into machine language. It is also said that it does not make much of a difference in the tests performed on some live web projects. For this I want to add a benchmark test published on the internet below. When we look at the benchmark tests, we can see the difference of JIT very well.

UNINON TYPES

This feature is previously known from other software languages such as C / C ++ or TypeScript. With PHP 8, we will be able to use this feature in PHP language.

Before PHP 8;

After PHP 8;

NULLSAFE OPERATOR

One of the most common things we do when developing projects is checking null. With PHP 7.0, the “Null coalescing operator” provided convenience for developers, but it could not be used with methods, so we were using classical methods again. In fact, languages such as Swift, C #, and JavaScript already had this feature. We can even say PHP is late.

NAMED ARGUMENTS

Let’s talk about one of the most appropriate features that will make code reading easier. I want to explain this issue as follows. We no longer have to write the names of parameters in methods and classes in order. We will be able to send the value by giving the name of the parameter. For example, let’s create a httpOnly cookie.

Before PHP 8;

It was necessary to write all the necessary parameters to create a cookie. In order to change the last parameter, I had to write all the parameters even if they were default values. This is a difficult, long and boring situation. With Named Argument, this situation is no longer necessary.

After PHP 8;

  • You can skip optional parameters

MATCH EXPRESSION

The match feature that comes with PHP 8 is an alternative to the switch. We can say that the switch is more advanced. The match command will provide us with a wide range of control possibilities and we can say that it has made the code reading even more beautiful.

Before PHP 8;

After PHP 8;

With the Match feature, we will be able to control multiple conditions to run the same code block.

No type coercion

STRING MATCH FUNCTIONS

With PHP 8 we got 3 new functions. Previously, we checked with strpos whether another string exists in a string. We can use these functions with PHP 8.

Before PHP 8;

After PHP 8;

str_starts_with: checks if the string starts with the value we specified, boolean value returned.
str_ends_with: checks if the string ends with the value we specified, boolean value returned.
str_contains: checks if the value we specified in the string is present, boolean value is returned.

NON CAPTURING CATCHES

Now that we catch an exception we do not have to assign a variable. When we use the Exception object, we can assign it to the variable.

Before PHP 8;

After PHP 8;

CONSTRUCTOR PROPERTY PROMOTION

Before PHP 8;

To get a value for the object in the constructor;
1. It is defined as Property
2. Defined as the Constructor parameter
3. The parameter taken in the Constructor would be defined to property.

After PHP 8;

I have brought together the innovations that I personally noticed in this article. However, there are other innovations / changes apart from these. You can use the resource here for more information. Apart from that, you can check the RFC lists on PHP’s official site.

I hope it was useful. Stay healthy and with code!

--

--