DEV Community

Cover image for Most Exciting Features Of PHP 7.4
Solace Infotech Pvt. Ltd.
Solace Infotech Pvt. Ltd.

Posted on

Most Exciting Features Of PHP 7.4

You may have used PHP as a programming language for a web development project and know the importance of its makeover to keep alive. The world of IT is continuously developing and hence programming technologies must be upgraded with the new features, updates and improvements for continuous success.

With the release of PHP 7.4, everything has changed. It was introduced on the 28th of November 2019. Before we dig deep to the PHP 7.4 features, let us see short basic of PHP.

PHP-

Php is a general-purpose scripting language meaning lines of codes that automate different tasks of web apps. These days, most of the businesses hire php developers to build a web app for their business. Developers can use server-side scripting abilities of PHP to build great web-apps. Also embedding PHP in HTML is used for web development, web apps, eCommerce apps and database applications. It can connect with different databases like MySQL, Oracle. With PHP one can execute the code from the server-side before it can get to the user’s browser.

Now let us see the new features in PHP 7.4.

Features In PHP 7.4 –

1. Typed properties’ support-
Among all the new features, it is one of the most important updated features in PHP 7.4. In previous versions of PHP, there was no provision to use declaration methods for class variables and properties. But now with PHP 7.4 developers can easily code without creation of getter and setter methods. Because of declaration types (excluding void and callable), developers can use nullable types, int, float, array, string, object, iterable, self, bool and parent. If a developer tries to assign an irrelevant value from the type, he will get a TypeError message. Typed properties allow developers to write code shorter and cleaner. For example,
<?php
class User{
public int $id;
Public string $name;
}
?>

2. Arrow Functions-
This function makes the syntax less verbose. Arrow functions are useful when you write callback functions. Just before PHP 7.4 you had to use array_map as follows.

$names = array_map(function($user) return $user -> name}, $users);
But, now with PHP 7.4…

$names = array_map(fn($user) => $user -> name, $users);
It’s just one line.

fn($user) => $user -> name;// ^^^^^^^^^^^^^^// This actually returns the value even there isn't a return statement
The form of the arrow functions is:

fn(params) => expression
The expression always a return statement even if the return keyword isn’t there.

The variable will be implicitly captured by-value, when you use the variable defined in parent scope in arrow function,

Nested arrow functions are also allowed.

$z = 1;$fn = fn($x) => fn($y) => $x * $y + $z;
$z in the parent scope, goes down the hierarchy to the most inner function. Type declaration is allowed on arguments and return types.

$fn = fn(int $x) : int => $x + 2; $fn(4); // 6$fn('4'); // 6 (due to implicit casting)$fn([4,5]); // error

3. Preloading-
Preloading is the process of loading files, frameworks, and libraries in OPcache. The main feature of this preloading feature is to improve the PHP 7.4 performance. It is a great addition to the new PHP release. If you use a framework, its files had to be downloaded and recompiled for each request.

When configuring the OPcache, at the first time these code files participate in the request processing and they are checked for changes each time. Preloading allows the server to load the specified code files into shared memory. Note that they will be continually accessible for every single request without extra checks for file changes. During preloading, PHP eliminates unnecessary includes and resolve class dependencies and links with traits, interfaces and so on.

4. Null Coalescing Assignment Operator-
Null Coalescing operator in PHP 7.4 brings a huge difference in PHP development because we can write the following code:

$data['name'] = isset($data['name']) ? $data['name'] : 'Guest';
simply as this:

$data['name'] = $data['name'] ?? 'Guest';
Now, It’s just:

$data['name']??= 'Guest';
All the above code does the same thing. Verify if the $data[‘name’] is set. If not, get “Guest” and assigns to $data[‘name’].

This is useful when you have some long variables.

$data['top_group']['people'][1]['name'] = $data['top_group']['people'][1]['name'] ?? 'Guest'; // ^^^ Repitition // can be written as $data['top_group']['people'][1]['name'] ??= 'Guest';
If the value of the left-hand side is null or not set, the value of the right-hand side will be assigned to the left.

5. Spread Operator-
A spread operator help PHP developers with two ways- first, It will be better than array_merge and second, it will help to optimize the compilation time for constant arrays.

6. Numeric Literal Separator-
Since PHP 7.4, you can add underscores in numeric variables.

//previously
$price= 631314582.67 //hard to read
//now
$price= 631_314_582.67 //easy to read
PHP will ignore the underscore and compile the PHP file. The main reason to add this feature is to allow developers to have more clean & comprehensible code. The Numeric Literal Separator can be used in any numeric type(integers, float, decimal, hexadecimal and binary).

7. Reflection For References-
Libraries like symfony/var-dumper strongly depend on ReflectionAPI to correctly display variables. Before PHP 7.4, there are no proper support for reference reflection, and hence these libraries rely on hacks to detect references. With the new version, PHP 7.4 adds ReflectionReference class that solves this issue.

8. Support for throwing exceptions from __toString()-
Prior to PHP 7.4, there was no ability to throw an exception from the _toString method. The reason behind this is, objects to strings conversion is performed in many functions of the standard library and not all of them are ready to “process” exceptions correctly. As a major aspect of this RFC, a comprehensive audit of string conversions in the codebase was done and this limitation was evacuated.

Final Words-
There are many new PHP features that reduce the memory usage and improve PHP 7.4 performance. Also you can write cleaner code and create web solutions rapidly. If you’re using an older version of PHP, you must upgrade and take advantage of the new features mentioned in the article.

Top comments (0)