What's new in PHP 7.4

What's new in PHP 7.4

Written by Gopakumar Gopalan,
on Sunday, January 5, 2020

PHP was around since 1995 as Personal Home Page. It gained popularity in early 2000 when PHP 5.2 LTS released in 2005. Many popular frameworks like Symfony, Zend  and CMS systems like Wordpress and Joomla is built around PHP. The updates to PHP were near to nothing all the years. Rumors of the death of PHP spread when they cancelled version 6 release which expected to have unicode features.

The things got drastically changed with PHP 7. When PHP 7.4 released in November 2019, PHP ensured it is not going anywhere and will stay as one of the popular language for web application development. Now PHP have a more frequent scheduled releases in pipeline and constant updates are expected to improve the overall PHP experience.

PHP 7.4 comes with long awaited features which will take it to the next level. Let’s explore what all added and changed in PHP 7.4.

Typed Properties

You can now add type declarations to class properties. This feature was one of the most asked for feature by developers around the world. 

class Todo {
    public int $id;
    public string $task;
    public bool $complete
}

Covariance and Contravariance

Those who are familiar to Typescript  better knows Covariance  and Contravariance. PH7.4 supports limited return type covariance and argument type contravariance. 

class A {}
class B extends A {}

class Master {
    public function method(): A {}
}
class Child extends Master {
    public function method(): B {}
}

Full variance support is only available if autoloading is used. Inside a single file only non-cyclic type references are possible, because all classes need to be available before they are referenced.

Arrow Functions

Much awaited fat arrow functions are now available from PHP 7.4. Earlier an array_map() function to add 5% VAT was written like this:

$vat = 1.05;
$prices = array_map(function($n) use ($vat) {
      return $n * $vat
}, [100, 200, 300, 400]);
// $prices = array(105, 210, 315, 420);

Now can be written with an arrow function in a single line of code like this:

$vat = 1.05;
$prices = array_map(fn($n) => $n * $vat, [100, 200, 300, 400]);
// $prices = array(105, 210, 315, 420);

Numeric Literal Separator

Numeric literals can contain underscores between digits as separators. See the example below:

6.674_083e-11; // float
299_792_458;   // decimal
0xCAFE_F00D;   // hexadecimal
0b0101_1111;   // binary

Null coalescing assignment operator

In earlier PHP versions if you need to assign something which is not yet set you use the following code :

$user = [
   'name' => 'John Doe',
   'bio' => 'I am filled with awesomeness'
];

if (!isset($user['profilepic'])) {
    $array['key'] = defaultProfilePic();
}

The same can be achieved now with the code below :

$user = [
   'name' => 'John Doe',
   'bio' => 'I am filled with awesomeness'
];

$user['profilepic'] ??= defaultProfilePic(); // Null coalescing assignment

Spread Operator in Array Expression

Now you can use spread operator to unpack arrays. You any follow the example.

$arr1 = [1, 2, 3];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]


function getArr() {
  return ['a', 'b'];
}
$arr6 = [...getArr(), 'c']; //['a', 'b', 'c']

function arrGen() {
 for($i = 11; $i < 15; $i++) {
  yield $i;
 }
}
$arr8 = [...arrGen()]; //[11, 12, 13, 14]

There are many additions like __serialize() and __unserialize() magic functions. You should follow the PHP migration guide to get your code PHP 7.4 ready.

PHP was around since 1995 as Personal Home Page. It gained popularity in early 2000 when PHP 5.2 LTS released in 2005. Many popular frameworks like Symfony, Zend  and CMS systems like Wordpress and Joomla is built around PHP. The updates to PHP were near to nothing all the years. Rumors of the death of PHP spread when they cancelled version 6 release which expected to have unicode features.

The things got drastically changed with PHP 7. When PHP 7.4 released in November 2019, PHP ensured it is not going anywhere and will stay as one of the popular language for web application development. Now PHP have a more frequent scheduled releases in pipeline and constant updates are expected to improve the overall PHP experience.

PHP 7.4 comes with long awaited features which will take it to the next level. Let’s explore what all added and changed in PHP 7.4.

Typed Properties

You can now add type declarations to class properties. This feature was one of the most asked for feature by developers around the world. 

class Todo {
    public int $id;
    public string $task;
    public bool $complete
}

Covariance and Contravariance

Those who are familiar to Typescript  better knows Covariance  and Contravariance. PH7.4 supports limited return type covariance and argument type contravariance. 

class A {}
class B extends A {}

class Master {
    public function method(): A {}
}
class Child extends Master {
    public function method(): B {}
}

Full variance support is only available if autoloading is used. Inside a single file only non-cyclic type references are possible, because all classes need to be available before they are referenced.

Arrow Functions

Much awaited fat arrow functions are now available from PHP 7.4. Earlier an array_map() function to add 5% VAT was written like this:

$vat = 1.05;
$prices = array_map(function($n) use ($vat) {
      return $n * $vat
}, [100, 200, 300, 400]);
// $prices = array(105, 210, 315, 420);

Now can be written with an arrow function in a single line of code like this:

$vat = 1.05;
$prices = array_map(fn($n) => $n * $vat, [100, 200, 300, 400]);
// $prices = array(105, 210, 315, 420);

Numeric Literal Separator

Numeric literals can contain underscores between digits as separators. See the example below:

6.674_083e-11; // float
299_792_458;   // decimal
0xCAFE_F00D;   // hexadecimal
0b0101_1111;   // binary

Null coalescing assignment operator

In earlier PHP versions if you need to assign something which is not yet set you use the following code :

$user = [
   'name' => 'John Doe',
   'bio' => 'I am filled with awesomeness'
];

if (!isset($user['profilepic'])) {
    $array['key'] = defaultProfilePic();
}

The same can be achieved now with the code below :

$user = [
   'name' => 'John Doe',
   'bio' => 'I am filled with awesomeness'
];

$user['profilepic'] ??= defaultProfilePic(); // Null coalescing assignment

Spread Operator in Array Expression

Now you can use spread operator to unpack arrays. You any follow the example.

$arr1 = [1, 2, 3];
$arr2 = [...$arr1]; //[1, 2, 3]
$arr3 = [0, ...$arr1]; //[0, 1, 2, 3]


function getArr() {
  return ['a', 'b'];
}
$arr6 = [...getArr(), 'c']; //['a', 'b', 'c']

function arrGen() {
 for($i = 11; $i < 15; $i++) {
  yield $i;
 }
}
$arr8 = [...arrGen()]; //[11, 12, 13, 14]

There are many additions like __serialize() and __unserialize() magic functions. You should follow the PHP migration guide to get your code PHP 7.4 ready.

Related Tags :

> @GKWrites > What's new in PHP 7.4