What’s New in PHP 7.4 (Features, Deprecations, Speed)

What’s New in PHP 7.4 (Features, Deprecations, Speed)

PHP 7.4 will be released on November 28, 2019. Its features include:

  • Arrow functions for cleaner one-liner functions
  • Preloading to improve performance
  • Typed properties in classes
  • Improved type variance
  • The null coalescing assignment operator as a shorthand
  • FFI for better extension development in PHP
  • Underscores can be used to format numeric values
  • Spread operator in arrays
  • And more, you can read about it here

New Features?

Arrow functions 

Arrow functions, also called “short closures”, allow for less verbose one-liner functions.

While you’d previously write this:

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

You can now write this:

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 line, which is also the return statement.

Typed properties

Class variables can be type hinted:

class A
{
    public string $name;
    
    public ?Foo $foo;
}

Improved type variance

I also wrote about PHP’s type system in the past, so it’s good to see some improvements are actually arriving in PHP’s core.

Type variance is another topic worth its own blog post, but in short: you’ll be able use covariant return types –

class ParentType {}
class ChildType extends ParentType {}

class A
{
    public function covariantReturnTypes(): ParentType
    { /* … */ }
}

class B extends A
{
    public function covariantReturnTypes(): ChildType
    { /* … */ }
}

– and contravariant arguments.

class A
{
    public function contraVariantArguments(ChildType $type)
    { /* … */ }
}

class B extends A
{
    public function contraVariantArguments(ParentType $type)
    { /* … */ }
}

Null coalescing assignment operator

Next is the null coalescing assignment operator, a shorthand for null coalescing operations. Instead of doing this:

$data['date'] = $data['date'] ?? new DateTime();

You can do this:

$data['date'] ??= new DateTime();

Array spread operator

Next up, it’s now possible to use the spread operator in arrays:

$arrayA = [1, 2, 3];
\
$arrayB = [4, 5];

$result = [0, ...$arrayA, ...$arrayB, 6 ,7];

// [0, 1, 2, 3, 4, 5, 6, 7]

Note that this only works with arrays with numerical keys.

Numeric Literal Separator

PHP 7.4 allows for underscores to be used to visually separate numeric values. It looks like this:

$unformattedNumber = 107925284.88;

$formattedNumber = 107_925_284.88;

Foreign function interface

Preloading

Custom object serialization

Reflection for references

Weak references

mb_str_split added

Password Hashing Registry

Left-associative ternary operator deprecation

Exceptions allowed in __toString

Concatenation precedence

PEAR not enabled by default 

About the author

Mantrasoft India administrator

Leave a Reply