当前位置:首页 > Web开发 > 正文

php7.4 更新特性

2024-03-31 Web开发

The PHP development team announces the immediate availability of PHP 7.4.0. This release marks the fourth feature update to the PHP 7 series.
PHP 7.4.0 comes with numerous improvements and new features such as:
Typed Properties
Arrow Functions
Limited Return Type Covariance and Argument Type Contravariance
Unpacking Inside Arrays
Numeric Literal Separator
Weak References
Allow Exceptions from __toString()
Opcache Preloading
Several Deprecations
Extensions Removed from the Core

1.Typed Properties 强类型

Class中预定义变量后, 只能赋值相应的数据类型

All types are supported, with the exception of void and callable:
class User {
public int $id;
public string $name;
}

They are also allowed with the var notation:
var bool $flag;
The same type applies to all properties in a single declaration:
public float $x, $y;

What happens if we make an error on the property type? Consider the following code:
class User {
public int $id;
public string $name;
}

$user = new User;
$user->id = 10;
$user->name = [];

Fatal error:
Fatal error: Uncaught TypeError: Typed property User::$name must be string, array used in /app/types.php:9

2.Arrow Function 箭头函数(简短闭包)
箭头函数为了闭包内只有一行代码的闭包函数提供了更简洁的方式

7.4.0之前
array_map(function (User $user) {
return $user->id;
}, $users)
之后
array_map(fn (User $user) => $user->id, $users)

function cube($n){
return ($n * $n * $n);
}
$a = [1, 2, 3, 4, 5];
$b = array_map(‘cube‘, $a);
print_r($b);
之后
$a = [1, 2, 3, 4, 5];
$b = array_map(fn($n) => $n * $n * $n, $a);
print_r($b);

$factor = 10;
$calc = function($num) use($factor){
return $num * $factor;
};
之后
$factor = 10;
$calc = fn($num) => $num * $factor;

3.Limited return type covariance and argument type contravariance 协变返回和逆变参数

子类和父类
协变返回
interface Factory {
function make(): object;
}

class UserFactory implements Factory {
// 将比较泛的 object 类型,,具体到 User 类型
function make(): User;
}

逆变参数
interface Concatable {
function concat(Iterator $input);
}

class Collection implements Concatable {
// 将比较具体的 Iterator参数类型,逆变成接受所有的 iterable类型
function concat(iterable $input) {/* . . . */}
}

4.Unpacking Inside Arrays 数组内解包
三个点...叫做Spread运算符, 第一个好处是性能
Spread 运算符应该比 array_merge 拥有更好的性能。这不仅仅是 Spread 运算符是一个语法结构,而 array_merge 是一个方法。还是在编译时,优化了高效率的常量数组

$parts = [‘apple‘, ‘pear‘];
$fruits = [‘banana‘, ‘orange‘, ...$parts, ‘watermelon‘];
var_dump($fruits);

array(5) { [0]=> string(6) "banana" [1]=> string(6) "orange" [2]=> string(5) "apple" [3]=> string(4) "pear" [4]=> string(10) "watermelon" }

RFC 声明我们可以多次扩展同一个数组。而且,我们可以在数组中的任何地方使用扩展运算符语法,因为可以在扩展运算符之前或之后添加普通元素。所以,就像下面代码所示的那样:
$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];
$arr3 = [...$arr1, ...$arr2];
$arr4 = [...$arr1, ...$arr3, 7, 8, 9];

也可以将函数返回的数组直接合并到另一个数组:
function buildArray(){
return [‘red‘, ‘green‘, ‘blue‘];
}
$arr1 = [...buildArray(), ‘pink‘, ‘violet‘, ‘yellow‘];

也可以写成生成器
function generator() {
for ($i = 3; $i <= 5; $i++) {
yield $i;
}
}
$arr1 = [0, 1, 2, ...generator()];

但是我们不允许合并通过引用传递的数组。 考虑以下的例子:
$arr1 = [‘red‘, ‘green‘, ‘blue‘];
$arr2 = [...&$arr1];

无论如何,如果第一个数组的元素是通过引用存储的,则它们也将通过引用存储在第二个数组中。 这是一个例子:
$arr0 = ‘red‘;
$arr1 = [&$arr0, ‘green‘, ‘blue‘];
$arr2 = [‘white‘, ...$arr1, ‘black‘];
上面这个是OK的

5.Numeric Literal Separator

数字字符间可以插入分隔符号
<?php
6.674_083e-11; // float
299_792_458; // decimal
0xCAFE_F00D; // hexadecimal
0b0101_1111; // binary
?>

Null Coalescing Assignment Operator
Added with PHP 7, the coalesce operator (??) comes in handy when we need to use a ternary operator in conjunction with isset(). It returns the first operand if it exists and is not NULL. Otherwise, it returns the second operand. Here is an example:

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/40679.html