PHP 7.4 New Features

PHP 7.4 release date is November 28, 2019. PHP 7.4 is a next basic release before PHP 8. PHP 7.4 comes with lots of new features. So it’s time for us to dive into some of the most exciting additions and new features that will make PHP faster and more reliable

  • PHP 7.4 brings spread operator in array expression
  • Arrow functions
  • Null Coalescing assignment operator
  • Weak References
  • Typed properties in classes
  • And more

Spread operator

Spread operator should have better performance than array_merge. That’s not only because the spread operator is a language structure while array_merge is a function, but also because of compile time optimization can be performance for constant arrays.

$name = ['ikhlaque', 'raheel'];
$test = ['A', 'B', ...$name, 'C'];
var_dump($test);

Output

array(5) {
 [0]=>
 string(6) "A"
 [1]=>
 string(6) "B"
 [2]=>
 string(5) "ikhalque"
 [3]=>
 string(4) "Raheel"
 [4]=>
 string(10) "C"
}

Arrow function

Arrow function also called a short closures allow for less verbose one-liner functions.

Previously we write as follwing

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

You can now write

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

Let’s summarise how short closures can be used.

  • They are available as of PHP 7.4
  • They start with the fn keyword
  • They can only have one expression, which is the return statement
  • No return keyword allowed
  • Arguments and return types can be type hinted
  • 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.

Null Coalescing assignment operator

Null Coalescing assignment operator ( ?? ) a shorthand for null

$username = $_GET['username'] ?? ‘everybody';

Weak References

Weak references it allow programmer to give reference to an object that doesn’t prevent the object itself from being destroyed. Currently PHP supports Weak References by using an extention like pecl-weakref. Anyway, the new API is different from the documented WeakRef class.

$object = new stdClass;
$weakRef = WeakReference::create($object);

var_dump($weakRef->get());
unset($object);
var_dump($weakRef->get());

Typed properties in classes

Typed properties add in PHP 7.4 and provided a major improvements. Type Hint feature since available from PHP 5. Now we can use them with Object data type. All type supported except NULL and VOID. Following is the very basic example

--- Example 1 ---
class User {
 public int $id;
 public string $name;
}

--- Example 2 ---
class User {
 public int $id;
 public string $name;
}

$user = new User;
$user->id = 10;
$user->name = [];

In the code above, we declared a string property type, but we set an array as property value. In such scenario, we get the following Fatal error:

Developer Diary

Share
Published by
Developer Diary

Recent Posts

Git Tag Cheat Sheet

Introduction Git tags are an essential feature of version control systems, offering a simple way…

2 months ago

Understanding Web Storage: Cookies, Local Storage

Introduction The methods that browsers employ to store data on a user's device are referred…

3 months ago

Setting up OpenVPN Access Server in Amazon VPC – AWS

Introduction A well-known open-source VPN technology, OpenVPN provides strong protection for both people and businesses.…

3 months ago

Enhance Error Tracking & Monitoring: Integrate Sentry with Node.js & Express.js

Introduction Integrating Sentry into a Node.js, Express.js, and MongoDB backend project significantly enhances error tracking…

3 months ago

Comparing Callbacks, Promises, and Async/Await in JavaScript

Introduction In the world of JavaScript development, efficiently managing asynchronous operations is essential. Asynchronous programming…

5 months ago

How To Secure Nginx with Let’s Encrypt on Ubuntu EC2 Instance

Introduction Let's Encrypt is a Certificate Authority (CA) that makes it simple to obtain and…

7 months ago