PHP 7.4 was released on November 28, 2019 and it’s the last version before PHP 8.
New features
PHP 7.4 comes with a remarkable amount of new features.
The following a list of all new features.
Arrow functions
Arrow functions, also called “short closures”, allow for less verbose one-liner functions.
1 2 3 4 5 6 7 |
// Previously array_map(function (User $user) { return $user->id; }, $users) // Now array_map(fn (User $user) => $user->id, $users) |
There are a few notes about arrow functions:
- They can always access the parent scope, there’s no need for the
use
keyword. $this
is available just like normal closures.- Arrow functions may only contain one expression, which is also the return statement.
Typed properties
Class variables can be type hinted:
1 2 3 4 5 6 |
class TypedHintClass { public string $name; public Foo $foo; } |