PHP 8.0 官方地址:https://www.php.net/releases/8.0/en.php
PHP 8.0 是PHP语言的一个重要更新。
它包含许多新特性和优化,包括命名参数、联合类型、属性、构造函数属性提升、匹配表达式、null安全运算符、JIT,以及类型系统、错误处理和一致性的改进。
那么 PHP 8 有哪些新特性呢?
命名参数(Named arguments)
只指定必需的参数,跳过可选参数。
参数是独立于顺序和自我记录的。
PHP 7:
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
PHP 8:
htmlspecialchars($string, double_encode: false);
属性(Attributes)
一般民间称之为注解
PHP 7:
class PostsController { /** * @Route("/api/posts/{id}", methods={"GET"}) */ public function get($id) { /* ... */ } }
PHP 8:
class PostsController { #[Route("/api/posts/{id}", methods: ["GET"])] public function get($id) { /* ... */ } }
值得一提的是,注解语法,在 PHP 7 中不会影响其它代码的执行,因为#在PHP中是注释符号。
构造函数属性提升(Constructor property promotion)
定义构造函数的同时定义属性,减少代码量,提升编码效率。
PHP 7:
class Point { public float $x; public float $y; public float $z; public function __construct( float $x = 0.0, float $y = 0.0, float $z = 0.0, ) { $this->x = $x; $this->y = $y; $this->z = $z; } }
PHP 8:
class Point { public function __construct( public float $x = 0.0, public float $y = 0.0, public float $z = 0.0, ) {} }
联合类型(Union types)
支持声明不止一个类型
PHP 7:
class Number { /** @var int|float */ private $number; /** * @param float|int $number */ public function __construct($number) { $this->number = $number; } } new Number('NaN'); // Ok
PHP 8:
class Number { public function __construct( private int|float $number ) {} } new Number('NaN'); // TypeError
匹配表达式(Match expression)
新匹配与switch类似,具有以下功能:
Match是一个表达式,意味着它的结果可以存储在变量中或返回。
匹配分支只支持单行表达式,不需要break;语句。
Match进行严格的比较。
PHP 7:
switch (8.0) { case '8.0': $result = "Oh no!"; break; case 8.0: $result = "This is what I expected"; break; } echo $result; //> Oh no!
PHP 8:
echo match (8.0) { '8.0' => "Oh no!", 8.0 => "This is what I expected", }; //> This is what I expected
NULL 安全运算符(Nullsafe operator)
现在,您可以使用新的nullsafe操作符来代替空检查条件。当对链中的一个元素求值失败时,整个链的执行将中止,整个链的计算结果为null。
PHP 7:
$country = null; if ($session !== null) { $user = $session->user; if ($user !== null) { $address = $user->getAddress(); if ($address !== null) { $country = $address->country; } } }
PHP 8:
$country = $session?->user?->getAddress()?->country;
更合理的字符串与数字比较(Saner string to number comparisons)
当与数字字符串进行比较时,PHP8使用数字比较。否则,它将数字转换为字符串并使用字符串比较。
PHP 7:
0 == 'foobar' // true
PHP 8:
0 == 'foobar' // false
内部函数的一致类型错误(Consistent type errors for internal functions)
如果参数验证失败,大多数内部函数现在都会抛出一个错误异常。
PHP 7:
strlen([]); // Warning: strlen() expects parameter 1 to be string, array given array_chunk([], -1); // Warning: array_chunk(): Size parameter expected to be greater than 0
PHP 8:
strlen([]); // TypeError: strlen(): Argument #1 ($str) must be of type string, array given array_chunk([], -1); // ValueError: array_chunk(): Argument #2 ($length) must be greater than 0
JIT
PHP8引入了两个JIT编译引擎。跟踪JIT是这两种方法中最有前途的一种,它在综合基准测试上的性能提高了大约3倍,在某些特定的长时间运行的应用程序上性能提高了1.5到2倍。典型的应用程序性能与php7.4不相上下。
JIT对php8性能的影响:
类型系统和错误处理的改进
对算术/位运算符进行更严格的类型检查
抽象性状法验证
魔术方法的正确签名
重新分类的发动机警告
不兼容方法签名的致命错误
@operator不再沉默致命错误。
私有方法继承
混合型
静态返回类型
内部函数类型电子邮件线程
不透明对象,而不是Curl、Gd、Sockets、OpenSSL、XMLWriter和XML扩展的资源
其他语法调整和改进
允许在参数列表和闭包使用列表中使用尾随逗号
非捕获捕获
可变语法调整
将名称空间名称视为单个标记
Throw现在是一个表达式
允许 $object::class
新的类、接口和函数
Weak Map class
Stringable interface
token_get_all() object implementation