PHP 8.0 是 PHP 语言的一个主版本更新 它包含了很多新功能与优化项, 包括命名参数、联合类型、注解、构造器属性提升、match 表达式、nullsafe 运算符、JIT,并改进了类型系统、错误处理、语法一致性。

命名参数

PHP 7

  1. htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);

PHP 8

  1. htmlspecialchars($string, double_encode: false);
  • 仅仅指定必填参数,跳过可选参数。
  • 参数的顺序无关、自己就是文档(self-documented)
  • 官方文档

注解

PHP 7

  1. class PostsController
  2. {
  3. /**
  4. * @Route("/api/posts/{id}", methods={"GET"})
  5. */
  6. public function get($id) { /* ... */ }
  7. }

PHP 8

  1. class PostsController
  2. {
  3. #[Route("/api/posts/{id}", methods: ["GET"])]
  4. public function get($id) { /* ... */ }
  5. }
  • 现在可以用 PHP 原生语法来使用结构化的元数据,而非 PHPDoc 声明
  • 官方文档

构造器属性提升

PHP 7

  1. class Point {
  2. public float $x;
  3. public float $y;
  4. public float $z;
  5. public function __construct(
  6. float $x = 0.0,
  7. float $y = 0.0,
  8. float $z = 0.0
  9. ) {
  10. $this->x = $x;
  11. $this->y = $y;
  12. $this->z = $z;
  13. }
  14. }

PHP 8

  1. class Point {
  2. public function __construct(
  3. public float $x = 0.0,
  4. public float $y = 0.0,
  5. public float $z = 0.0,
  6. ) {}
  7. }
  • 更少的样板代码来定义并初始化属性
  • 官方文档

联合类型

PHP 7

  1. class Number {
  2. /** @var int|float */
  3. private $number;
  4. /**
  5. * @param float|int $number
  6. */
  7. public function __construct($number) {
  8. $this->number = $number;
  9. }
  10. }
  11. new Number('NaN'); // Ok

PHP 8

  1. class Number {
  2. public function __construct(
  3. private int|float $number
  4. ) {}
  5. }
  6. new Number('NaN'); // TypeError
  • 相较于以前的 PHPDoc 声明类型的组合, 现在可以用原生支持的联合类型声明取而代之,并在运行时得到校验
  • 官方文档

Match 表达式

PHP 7

  1. switch (8.0) {
  2. case '8.0':
  3. $result = "Oh no!";
  4. break;
  5. case 8.0:
  6. $result = "This is what I expected";
  7. break;
  8. }
  9. echo $result;
  10. //> Oh no!

PHP 8

  1. echo match (8.0) {
  2. '8.0' => "Oh no!",
  3. 8.0 => "This is what I expected",
  4. };
  5. //> This is what I expected

新的 match 类似于 switch,并具有以下功能:

  • Match 是一个表达式,它可以储存到变量中亦可以直接返回。
  • Match 分支仅支持单行,它不需要一个 break; 语句。
  • Match 使用严格比较
  • 官方文档

Nullsafe 运算符

PHP 7

  1. $country = null;
  2. if ($session !== null) {
  3. $user = $session->user;
  4. if ($user !== null) {
  5. $address = $user->getAddress();
  6. if ($address !== null) {
  7. $country = $address->country;
  8. }
  9. }
  10. }

PHP 8

  1. $country = $session?->user?->getAddress()?->country;
  • 现在可以用新的 nullsafe 运算符链式调用,而不需要条件检查 null。 如果链条中的一个元素失败了,整个链条会中止并认定为 Nul
  • 官方文档

字符串与数字的比较更符合逻辑

PHP 7

  1. 0 == 'foobar' // true

PHP 8

  1. 0 == 'foobar' // false
  • PHP 8 比较数字字符串(numeric string)时,会按数字进行比较。 不是数字字符串时,将数字转化为字符串,按字符串比较
  • 官方文档

内部函数类型错误的一致性

PHP 7

  1. strlen([]); // Warning: strlen() expects parameter 1 to be string, array given
  2. array_chunk([], -1); // Warning: array_chunk(): Size parameter expected to be greater than 0

PHP 8

  1. strlen([]); // TypeError: strlen(): Argument #1 ($str) must be of type string, array given
  2. array_chunk([], -1); // ValueError: array_chunk(): Argument #2 ($length) must be greater than 0
  • 现在大多数内部函数在参数验证失败时抛出 Error 级异常
  • 官方文档