PHP7.4 新特性

原文地址: PHP7.4 新特性

属性添加限定类型

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

class User
{
public int $age;
public string $name
}

$user = new User();
$user->age = 10;
$user->name = "张三";
// error 需要传递int
$user->age = "zhang";

箭头函数

这个特性基本上参考 JsES6 的语法。可以让我们的代码写的更少。如果你的代码有 fn 这个函数。可能会冲突

1
2
3
4
5
6
7
8
9
<?php

$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3]); // [10,20,30]

// 之前的写法
$nums = array_map(function ($num) use ($factor) {
return $num * $factor;
}, [1, 2, 3])

有限返回类型协变与参数类型逆变

仅当使用自动加载时,才提供完全协变 / 逆变支持。在单个文件中,只能使用非循环类型引用,因为所有类在被引用之前都必须可用。

1
2
3
4
5
6
7
8
9
10
11
<?php

class A {}
class B extends A {}

class Producer {
public function method(): A {}
}
class ChildProducer extends Producer {
public function method(): B {}
}

数组解包

使用展开运算符 ... 解包数组。这个特性,应该又是从 js 那吸收过来的。看例子

1
2
3
4
5
6
7
8
<?php

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

// 老的写法
$fruits = array_merge(['banana', 'orange'], $parts, ['watermelon']);

空合并运算符赋值

1
2
3
4
5
6
7
8
<?php

$array['key'] ??= computeDefault();

// 老的写法
if (!isset($array['key'])) {
$array['key'] = computeDefault();
}

数值文字分隔符

数字文字可以在数字之间包含下划线。

1
2
3
4
5
6
<?php

6.674_083e-11; // float
299_792_458; // decimal
0xCAFE_F00D; // hexadecimal
0b0101_1111; // binary

允许从 __toString () 抛出异常

现在允许从 __toString() 引发异常,以往这会导致致命错误,字符串转换中现有的可恢复致命错误已转换为 Error 异常。

Filter

  • 新增 FILTER_VALIDATE_FLOAT
1
2
3
<?php

filter_var(1.00, FILTER_VALIDATE_FLOAT);

strip_tags 支持数组

1
2
3
4
5
6
<?php

strip_tags($str,['p','a','div']);

// 老的写法
strip_tags($str,"<p><a><div>");

废弃的特性

没有显式括号的嵌套三元运算符

1
2
3
4
5
<?php

1 ? 2 : 3 ? 4 : 5; // deprecated
(1 ? 2 : 3) ? 4 : 5; // ok
1 ? 2 : (3 ? 4 : 5); // ok

花括号访问数组索引

1
2
3
4
5
6
<?php

$arr = ["a"=>"111"];
$index = "a";
$arr{$index}; // 废弃
$arr[$index];

real 和 is_real 实数

1
2
3
4
5
<?php

$num = "";
$a = (real) $num; // 废弃
$a = (float) $num;

parent 关键词在没父类的类中使用

在没有父类的类中使用 parent 会出现编译错误。

1
2
3
4
5
6
7
8
9
<?php

class Test
{
public function index()
{
return parent::index(); //编译错误
}
}

money_format 函数

money_format 被废弃,使用 numberFormater 替换

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2023 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :