Back to Top

What’s new in PHP 7.4

Updated 8 May 2020

PHP give developer more usability and maintenance, Recently PHP has released our 7.4 version on November 28, 2019. With PHP 7.4, we can use some interesting features like arrow function, spread operator etc.

PHP 7.4 Features List

Arrow Function

Arrow function will provide a shorthand syntax for defining functions with implicit by-value scope binding.

Arrow function will provide you simplicity in your code, We look out these example

Example 1, before PHP 7.4 :-

$arr = [1, 2, 3, 4, 5];

function cube( $c ){
  return ( $c * $c * $c );

$new_arr = array_map( 'cube', $arr );

print_r( $new_arr );

With PHP 7.4, we will get this :-

Start your headless eCommerce
Find out More
$arr = [1, 2, 3, 4, 5];

$new_arr = array_map( fn( $c ) => $c * $c * $c, $arr );

print_r( $new_arr );

By use language construct with anonymous function, we can inherit parent scope like this

Example 2, before PHP 7.4:-

$fac = 10;

$calc = function( $n ) use( $fac ){
	return $n * $fac;

But with PHP 7.4, We can do same things like this :-

$fac = 10;

$calc = fn( $n ) => $n * $fac;

Covariant Returns and Contravariant Parameters

Currently, PHP uses mostly invariant parameter types and return types. Meaning, if a method has a parameter or return type of X then the subtype parameter or return type must also be type X.

Now, with PHP 7.4 it proposes to allow covariant (ordered from specific to generic) and contravariant (reversing the order) on parameter and return types.

For E.g :-

Covariant return type

interface Factory {
 function make(): object;

class UserFactory implements Factory {
 function make(): User;

Contravariant parameter type

interface Concatable {
 function concat(Iterator $input); 

class Collection implements Concatable {
 // accepts all iterables, not just Iterator
 function concat(iterable $input) {/* . . . */}

Coalescing Assign Operator

PHP 7.4 introduce an another new feature Coalescing Assign Operator. It’s next version of ternary operator together with isset(). By using this we can get first operand if it exists else get second operand.

For E.g :-

$username = $_GET['user_name'] ?? ‘something else';

Spread Operator in Array Expression

In PHP 7.4, Now we can use spread operators in arrays, This expression is faster than array_merge function.

Here is an example

$arrayA = [1, 2, 3];

$arrayB = [4, 5];

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

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


The main purpose of this cool new feature is to increase PHP 7.4 performance. Simply put, preloading is the process of loading files, frameworks, and libraries in OPcache and is definitely a great addition to the new release. For example, if you use a framework, its files had to be downloaded and recompiled for each request.

When configuring OPcache, for the first time these code files participate in the request processing and then they are checked for changes each time. Preloading enables the server to load the specified code files into shared memory. It’s important to note that they will be constantly available for all subsequent requests without additional checks for file changes.

It is also noteworthy to mention that during preloading, PHP also eliminates needless includes and resolves class dependencies and links with traits, interfaces, and more.

Numeric Literal Separator

By using Numeric Literal Separator, we can seperate numeric values by underscores.

For E.g :-

$unformattedNumber = 107925284.88;

$formattedNumber = 107_925_284.88;

The underscores are simply ignored by the engine.

Weak References

By using weak references the programmer allow to retain a reference to an object that does not prevent the object from being destroyed.

Custom Object Serialization

A new mechanism for custom object serialization has been added, which uses two new magic methods: __serialize and __unserialize.

// Returns array containing all the necessary state of the object.
public function __serialize(): array;

// Restores the object state from the given data array.
public function __unserialize(array $data): void;

The new serialization mechanism supersedes the Serializable interface, which will be deprecated in the future.


The following list provides a short overview of the functionality targeted for deprecation

  • The real type
  • Magic quotes legacy
  • array_key_exists with objects
  • Reflection export() methods
  • mb_strrpos() with encoding as 3rd argument
  • implode() parameter order mix
  • Unbinding $this from non-static closures
  • hebrevc() function
  • convert_cyr_string() function
  • money_format() function
  • ezmlm_hash() function
  • restore_include_path() function
  • allow_url_include ini directive

Thanks for reading this blog.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home

Table of Content