The KoolReport Blog

Announcements, discussions, and more for KoolReport and its extended packages.

PHP 7.4: Top 5 Advanced Features That You Need To Know

PHP 7.4 has been released in Nov 28th, 2019 with new syntax and significant performance enhancement. Let look at the top 5 cool features of PHP 7.4 that you should know.

PHP 7.4 Top 5 advanced features

Since PHP7, we have seen the incredible changes in PHP from its syntax improvement to its speed and efficiency. Since the emergence of new language like NodeJS, GoLang, there are always rumor whether PHP is going to die but believe me PHP still move forward and be stronger day by day. It is no wonder that for the second year in a row, PHP is among the top 10 most popular programming languages according to StackOverflow Developer Survey 2019. This year, it took the 8th place which is one rank higher than in the previous year.

PHP 7.4 has been released in Nov 28th, 2019 with new syntax and significant performance enhancement. Let look at the most top 5 cool features of PHP 7.4 that you need to know. Let started!

Arrow function

If you are using ES6 in Javascript, probably you have been familiar with arrow function. The arrow function helps to reduce syntax when we write anonymous functions. So now instead of writing this:

function do_double($n)
{
    return $n*$n;
}
$a = [1,2,3];
$b = array_map("do_double",$a);

With PHP 7.4, you can do:

$a = [1,2,3];
$b = array_map(fn($n) => $n*$n, $a);

As a result, your code will look cleaner & neater, allowing you to save much development & debugging time.

Typed properties

This new feature is considered one of the most important features in PHP 7.4. Previously you need to use getter and setter in order to check types of variables. Now you can easily specify type for class's variables:

class SaleReport
{
    protected int $month;
}

Due to declaration types (excluding void and callable), you can use nullable types, int, float, array, string, object, iterable, self, bool, and parent.

If developer try to assign irrelevant value from the type, he or she will get TypeError message.

The same like arrow function, typed properties will reduce work for developer and make code cleaner and shorter.

Preloading

Finally, preloading is supported in PHP, which will help to improve performance. As you may know, each time you page is requested, the code files will need to read and loaded into memory to execute. Preloading, in simple word is process of loading code files into OPcache to execute faster. It will eliminate I/O process which consume time and resource, hence your page will response quickly.

When you use OPcache, the executed files will be first loaded into memory and will be checked for changes for next time. Preloading will let developer specify certain files to be loaded into memory and stay there. It will help to save time for OPcache to check changes in those files, thus, increase the performance.

PHP 7.4 Preloading performance comparison

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.

Covariant & Contravariant

At the moment, PHP has mostly invariant parameter types and invariant return types which presents some constraints. With the introduction of covariant (types are ordered from more specific to more generic) returns and contravariant (types are ordered from more generic to more specific) parameters, PHP developers will be able to change the parameter’s type to one of its supertypes. The returned type, in turn, can be easily replaced by its subtype.

Coalescing assign operator

Below are the most common code that you will see in almost all projects:

$country = isset($_GET['country']) ? $_GET['country'] : 'unknown';

With PHP 7.4, now you can do:

$country = $_GET['country'] ?? 'unknown';

or a little complicated, you can do:

$country = $_GET['country'] ?? $_POST['country'] ?? 'unknown';

A spread operator

Another interesting feature in PHP 7.4 is the spread operator in array. Previously, you may need to use array_merge() to complete in complicated way. But no more, you can do this in PHP 7.4.

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];

As you notice the ..., the array $part will be spread at right place in $fruits array. This is another feature that help developer to produce cleaner code and also save time on debugging.

Conclusion

There are several other updates in PHP 7.4 that we have not covered yet. Above are the features that we think the most important and that we need to know. As you may be aware that we are working on KoolReport, an PHP framework specializing in data report so keeping update with new features of PHP is a must for us. By utilizing new PHP features, we can improve the way data report should be created and ultimately make developer's life easier.

php

KoolReport helps to analyze your data and ultimately turn them into visual reports or dynamic dashboards.

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"

Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great product and amazing."

Dr. Lew Choy Onn

"Fantastic framework for reporting!"

Greg Schneider
Get KoolReport Now, It's FREE!