php 7.4

Admin   PHP   370  2020-08-13 08:42:32

PHP 7.4 is the latest PHP version released on November 28, 2019. In this article, I will talk about some new features, some ways to tweak performance, and why you should move to PHP 7.4.

PHP is constantly thriving. By releasing this latest PHP 7.4 update, it has added a lot of new features. As with previous PHP 7 versions - performance and speed are both improved. One of the best new features is features is preloading. It helps speed up script execution, and also makes the code cleaner, thanks to simplifying the common code.

php 7.4

PHP 7.4

PHP 7.4 has proven to be better than PHP 7.3, in terms of performance and other significant improvements.

What's new in PHP 7.4?

Since 2016, PHP7 has been upgraded every year. Each year it has new features, and features that help to write code more neatly, making the programming language more developer-friendly.

Preloading

When using a framework or library, files are uploaded and linked to each request. Preloading is when you load frameworks and libraries into OPCache. It helps the server download the PHP file first and store it in memory on startup to be available for any future requests!

Preloading is run by php.ini with the instruction: opache.preload. It forces PHP scripts to run when the server starts up. It can also be used to upload other files or choose to include or compile them. Preloaded files also stay in OPCache memory forever. These preload files are also available for future requests if needed.

Spread Operator in Array Expressions

When PHP 5.6 was released, PHP began to support unpacking (spread operator) argument. For now, with 7.4, we can use this feature with an array expression. To do so, it only needs to use ... (3 dots).

Take a look at the example below:

$arr1 = ['3', '4'];
$arr2 = ['1', '2', ...$arr1, '5'];
// ['1', '2', '3', '4', '5'];

Now we can extend the array from anywhere in another array, just using the Spread Operator syntax.

$num1 = [1, 2, 3];
$num2 = [...$num1]; // [1, 2, 3]
$num3 = [0, ...$num1]; // [0, 1, 2, 3]
$num4 = array(...$num1, ...$num2, 111); // [1, 2, 3, 1, 2, 3, 111]
$num5 = [...$num1, ...$num1]; // [1, 2, 3, 1, 2, 3]
function getNum(){
    return ['1', '2', '3'];
}
$num = [...getNum(), '4', '5', '6'];

// ['1', '2', '3', '4', '5', '6'];

Bread operators have better performance than version 7.3 array_merge. This is because the spread operator is a structural language while array_merge is a function. Also because spread operators support horizontal deployment objects, array_merge only supports arrays.

Weak References

PHP 7.4 now has the WeakReference class, not to be confused with the WeakRed and Weakref extension classes.

WeakReferences helps the programmer call a reference to an object. It is useful because it does not prevent the object from being destroyed. Support for structured cache implementations.

WeakReference {
/* Methods */
    public __construct ( void )
    public static create ( object $ref ) : WeakReference
    public get ( void ) : ?object
}

Contravariant Parameters and Covariant Returns

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

Now with PHP 7.4, it allows covariant (sorted from detail to overview) and contravariant (vice versa) on parameter and return types.

Here is an example of both:

For example return type of covariant:

interface Factory {
    function make(): object;
}

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

Example contravariant parameter type:

interface Concatable {
    function concat(Iterator $input);
}
class Collection implements Concatable {
    function concat(iterable $input) {/* . . . */}
}

Typed Properties 2.0

Since PHP 5, type hints have been supported, allowing a certain type of variable to be passed to a function or class. When upgrading to PHP 7.2, data type objects appear, and other types may be more awaited in the future. The future is here now

In the new version 7.4, PHP can support the following:

bool, int, float, string, array, object, iterable, self, parent
any class or interface name
?type

Arrow Functions 2.0

The arrow function now has a much simpler form:

fn(parameter_list) => expr

Things removed on PHP 7.4

    Type real
    Magic quotes legacy
    array_key_exists() with objects
    Filter FILTER_SANITIZE_MAGIC_QUOTES
    Reflection export method()
    mb_strrpos() with encode as the third argument
    Parameter implode() order mix
    Unbinding $this from non-static closures
    Hebrevc() function
    Convert_cyr_string ()
    money_format() function
    ezmlm_hash() function
    restore_include_path() function
    allow_url_include ini directive

Reference source

https://www.php.net/releases/7_4_0.php