The PHP development team has just released the PHP 7.4.0 version on the 28th of November 2019.

This version includes several improvements and new features that we will talk about the important ones.

1. Typed Properties

Class properties now support type declarations:

<?php
class User {
    public int $id;
    public string $name;
}
?>

As you can see, this piece of code will enforce that the _$user->id_can **only **be assigned integer values and _$user->name_ can onlybe assigned string values.

2. Unpacking inside arrays

<?php
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
?>

In the example above, the variable $parts is an array that is used in the $fruits array, and the unpacking process is done in the $fruits array. It’s an alternative for array_merge function, but the advantage of using the new syntax is its speed.

3. Arrow functions

Arrow functions provide a shorthand syntax for a one-liner function. These functions can be called “ short closures”.

Before PHP 7.4.0:

array_map(function (User $user) {
    return $user->id;  
}, $users)

Now:

array_map(fn (User $user) => $user->id, $users)

#php #software-engineering #oop #php7

What’s new in PHP 7.4?
1.35 GEEK