What’s new in PHP 7.4?

What’s new in PHP 7.4?

06 October 2020

Since the arrival of PHP 5 out of 2004, its performance has multiplied and maybe, even significantly increased. PHP 7.4 brings loads of new highlights, language structure increments and fixes. It was discharged on November 28, 2019. In this post you’ll find a list of PHP 7.4 features that are new and will help you upgrade. How about we start with a couple of features, arrised in PHP 7.4:

  • Typed properties
  • Arrow functions
  • Preloading
  • Return type covariance and augmented type contravariance
  • Null coalescing assignment operator
  • Array spread operator
  • Numeric literal separator

Typed Properties

The presentation of typed properties in the next release will probably be viewed as one of the most significant refreshed PHP highlights. While already there was no likelihood to utilize declaration methods for class variables and properties.

If we attempt to assign an irrelevant value to a variable, for instance, declaring $name as string, then will get a TypeError message.

Few things to note:

  1. They are only available in classes and require an access modifier: public, protected or private; or var
  2. All types are allowed, except void and callable

This is what it look like:

Class Foo

{

    public int $a;

}

Arrow functions

Since anonymous functions, or closures, are basically applied in JS, they appear to be verbose in PHP. Their execution and upkeep methods are likewise increasingly unpredictable. Arrow functions are also called “short closures”

The presentation of bolt capacities’ help will empower PHP designers to significantly tidy up their code and make the grammar progressively brief. Take an example below:

Previously you had to write:

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 couple of notes about arrow works:

  1. They can generally get to the parent scope, there’s no requirement for the utilization use keyword.
  2. $this is accessible simply like typical closures.
  3. Arrow function may just contain one expression, which is return statement.
  4. No return keyword allowed
  5. Arguments and return types can be type hinted

A more accurate way of writing the example above could be this:

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

Preloading

A component that could improve the exhibition of your code altogether. Preloading is the way toward stacking documents, systems, and libraries in OPcache and is certainly an extraordinary expansion to the new version. For instance, on the off chance that you utilize a system, its records must be downloaded and recompiled for each request.

While configuring OPcache, just because these code records take an interest in the request handling and afterward they are checked for changes each time. Preloading empowers the server to stack the predefined code documents into shared memory. Note that they will be continually accessible for every single consequent request without extra checks for document changes.

PHP disposes of unnecessary includes and resolves class dependencies and links with traits, interfaces, and more.

Basically, this is the means by which it works:

  1. So as to preload records, you have to compose a custom PHP content
  2. This content is executed once on server startup
  3. All preloaded records are accessible in memory for all request
  4. Changes made to preloaded records won’t have any impact until the server is restarted

Limited return type covariance and argument type contravariance

Right now, PHP has, for the most part, invariant parameter types and invariant return types which shows a few limitations. With the presentation of covariant returns and contravariant parameters. PHP developers will be able to change the parameter’s type to one of its supertypes. The returned type can be easily replaced by its subtype.

You’ll be able use covariant return types –

class ParentClassType {}

class ChildClassType extends ParentClassType {}




class Foo

{

    public function covariantReturnTypes(): ParentClassType

    { /* Method Definition */ }

}




class Bar extends Foo

{

    public function covariantReturnTypes(): ChildClassType

    { /* Method Definition */ }

}

And contravariant arguments.

class Foo

{

    public function contraVariantArguments(ChildClassType $type)

    { /* Method Definition */ }

}




class Bar extends Foo

{

    public function contraVariantArguments(ParentClassType $type)

    { /* Method Definition */ }

}

Null coalescing assignment operator

The null coalescing operator (??) has been included as syntactic sugar for the regular instance of expecting to utilize a ternary related to isset(). It returns the first operand if it is available, otherwise it returns second operand.

Previously, you need to write below code

$data['date'] = isset($data['date']) ? $data['date'] : date(‘Y-m-d’);

You can now write this:

$data['date'] = $data['date'] ?? date(‘Y-m-d’);

Array spread operator

PHP 7.4 will enable architects to utilize spread operators in arrays that are quicker contrasted with array_merge. There are two key purposes behind that. First, a spread operator is viewed as a language structure and array_merge is a function. The second is that your compile-time can be optimized for constant arrays. It will also increase performance.

Take a look at the example

$firstSubArray = [1, 2, 3];

$secondSubArray = [4, 5, 6, 7];

$result = [0, ...$firstSubArray, ...$secondSubArray, 8 ,9];




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

Numeric Literal Separator

PHP 7.4 takes into account underscores to be utilized to separate numeric values. It would seem that this:

$number = 989898978.88;

$formattedNumber = 989_898_978.88;

The underscores are ignored by the engine.

Other Changes

You ought to consistently investigate the full UPGRADING report while updating PHP forms.

search
Blog Categories
Request a quote