Best new features in PHP 7.4

Best new features in PHP 7.4

On 28th of november PHP 7.4 has been released. Here’s my favourite features!

Spread Operator for Arrays

No more array_merge(). With the spread operator you will be able to merge arrays faster. Example:

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];

Bad news is you can only use indexed arrays – no string keys!

 Arrow Functions 2.0

Arrow functions, AKA “short closures”, allow for less verbose one-liner functions: previously you had to write

$greeting = 'Hello';
$greet = function($name) use ($greeting) {
    echo ($greeting . " " . $name);
}

Now you can write it this way:

$greeting = 'Hello';
$greet = fn($name)  =>  echo ($greeting . " " . $name);

Note there’s no need for the use keyword. The arrow function has always access to its parent’s scope!

Null Coalescing Assignment Operator

Despite ?? coalescing operator being a comparison operator, coalesce equal ??= operator is an assignment operator. If the left parameter is null, assigns the value of the right paramater to the left one. If the value is not null, nothing is made:

// The folloving lines are doing the same
$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';
// Instead of repeating variables with long names, the equal coalesce operator is used
$this->request->data['comments']['user_id'] ??= 'value';

Typed Properties

We can now  declare types for class properties. So this:

class User {
    /** @var int $id */
    private $id;
    /** @var string $name */
    private $name;
}

becames this:

class User {
    public int $id;
    public ?string $name; //it can be null
}

All types are supported except void and callable. Even static properties are supported.
Property types are invariant. This means that the type of a (non-private) property is not allowed to change during inheritance (this includes adding or removing property types). If the parent property is private, then the type may be changed arbitrarily.

class A {
    private bool $a;
    public int $b;
    public ?int $c;
}
class B extends A {
    public string $a; // legal, because A::$a is private
    public ?int $b;   // ILLEGAL
    public int $c;    // ILLEGAL
}

Default values for typed properties have to match the type of the property. The only exception is that float properties also accept integer default values.

If a typed property does not have a default value, no implicit null default value is implied (even if the property is nullable)

Preload

My favourite PHP 7.4 new feature is preload.  On server startup – before any application code is run – we may load a certain set of PHP files into memory – and make their contents permanently available to all subsequent requests that will be served by that server. All the functions and classes defined in these files will be available to requests out of the box, exactly like internal entities. In this way, we may preload entire or partial frameworks, and even the entire application class library.
But how does it work?
Preloading is controlled by a new php.ini directive – opcache.preload. Using this directive we will specify a single PHP file – which will perform the preloading task. Once loaded, this file is then fully executed – and may preload other files, either by including them or by using the opcache_compile_file() function

Leave a Reply

Your email address will not be published. Required fields are marked *