PHP 7.4.0 Release

PHP 7.4.0 was released, and this release marks the fourth feature update in the PHP 7 series. PHP 7.4.0 has made many improvements and brought with it a number of new features, including:

Typed Properties

Class properties now support type declarations, and the following example forces $User-id to assign only int values, while $User-name can only assign string values.

<?phpclassUser{    public int $id;    public string $name;}?>

Arrow Functions

Arrow functions provide a shorthand syntax for defining functions that have implicit value-by-value scope bindings.

<?php$factor = 10;$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);// $nums = array(10, 20, 30, 40);?>

It can be extremely useful when passing closures to functions such as array_map or array_filter.

// A collection of Post objects $posts = [/* … */];$ids = array_map(fn($post) => $post->id, $posts);

Limited Return Type Covariance and Argument Type Contravariance Limited Return Type Covariance and Parameter Type Inversion

Full covariance/inversion support is only available when automatic loading is used. In a single file, only non-circular type references can be used because all classes must be available before they can be referenced.

<?phpclassA{}classBextendsA{}classProducer{    publicfunctionmethod(): A{}}classChildProducerextendsProducer{    publicfunctionmethod(): B{}}?>

Unpacking Inside Arrays Packs Internal Arrays

<?php$parts = [‘apple’, ‘pear’];$fruits = [‘banana’, ‘orange’, …$parts, ‘watermelon’];// [‘banana’, ‘orange’, ‘apple’, ‘pear’, ‘watermelon’];?>

Numeric Literal Literal literal text separator

Numeric text can underline numbers.

<?php6.674_083e-11; // float299_792_458;   // decimal0xCAFE_F00D;   // hexadecimal0b0101_1111;   // binary?>

Weak References

Weak references allow programmers to retain references to objects without preventing them from being destroyed.

Allow Exceptions from __toString() allows exceptions to be thrown from __toString()

An exception is now allowed to be thrown from __toString(), which in the past resulted in a fatal error in a string conversion where an existing recoverable fatal error has been converted to an Error exception.

Opcache Preload

New Opcache preload support.

There are also some deprecations, as well as some extensions removed from the core, see more:

https://www.php.net

https://www.php.net/manual/zh/migration74.new-features.php

Add a Comment

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