扫码阅读
手机扫码阅读

PHP8正式版发布,带来了注解和JIT

128 2024-01-25

PHP8 正式版已经发布,它引入了一些重大变更,以及许多新特性和性能优化,包括命名参数、联合类型、注解、Constructor Property Promotion、match 表达式、nullsafe 运算符、JIT,以及对类型系统、错误处理和一致性的改进。

之前的 PHPCon 上听过 Nikic 的一些分享,感兴趣的小伙伴可以查看Nikic 的 PPT

PHP8

在 PHP 官网 也提到了一些新特性和功能说明,我们来看一看

命名参数

Named arguments

//PHP7 htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false); //PHP8 htmlspecialchars($string, double_encode: false); 
  • 只指定必需的参数,跳过可选参数。

  • 参数是独立于顺序和自我记录的。

属性

Attributes ,也就是我们常说的注解,而且语法不会影响低版本,因为 # 在 PHP 中是注释符号

//PHP7 class PostsController { /**
     * @Route("/api/posts/{id}", methods={"GET"})
     */ public function get($id) { /* ... */ }
} //PHP8 class PostsController { #[Route("/api/posts/{id}", methods: ["GET"])] public function get($id) { /* ... */ }
} 

构造函数属性提升

Constructor property promotion ,让我们在定义构造函数的同时定义属性,减少代码量,提升编码效率

//PHP7 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;
  }
} //PHP8 class Point { public function __construct(
    public float $x = 0.0,
    public float $y = 0.0,
    public float $z = 0.0,
  ) {}
} 

联合类型

Union types ,支持声明不止一个类型

//PHP7 class Number { /** @var int|float */ private $number; /**
   * @param float|int $number
   */ public function __construct($number) { $this->number = $number;
  }
} new Number('NaN'); // Ok //PHP8 class Number { public function __construct(
    private int|float $number
  ) {}
} new Number('NaN'); // TypeError 

匹配表达式

Match expression ,这个鸟哥也发过文章说过:《PHP8 新特性之 match 表达式》

新匹配与 switch 类似,具有以下功能:

Match 是一个表达式,意味着它的结果可以存储在变量中或返回。

匹配分支只支持单行表达式,不需要break;语句。

Match 进行严格的比较。

//PHP7 switch (8.0) { case '8.0':
    $result = "Oh no!"; break; case 8.0:
    $result = "This is what I expected"; break;
} echo $result; //> Oh no! //PHP8 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

这个特性确实挺不错的,减少了不少代码量和逻辑代码

//PHP7 $country = null; if ($session !== null) {
  $user = $session->user; if ($user !== null) {
    $address = $user->getAddress(); if ($address !== null) {
      $country = $address->country;
    }
  }
} //PHP8 $country = $session?->user?->getAddress()?->country; 

更合理的字符串与数字比较

Saner string to number comparisons ,当与数字字符串进行比较时,PHP8 使用数字比较。否则,它将数字转换为字符串并使用字符串比较

//PHP7 0 == 'foobar' // true //PHP8 0 == 'foobar' // false 

内部函数的一致类型错误

Consistent type errors for internal functions ,如果参数验证失败,大多数内部函数现在都会抛出一个错误异常

//PHP7 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 //PHP8 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 感兴趣的可以看鸟哥的博客《PHP 8 新特性之 JIT 简介》

PHP8 引入了两个 JIT 编译引擎。跟踪 JIT 是这两种方法中最有前途的一种,它在综合基准测试上的性能提高了大约 3 倍,在某些特定的长时间运行的应用程序上性能提高了 1.5 到 2 倍。典型的应用程序性能与 PHP7.4 不相上下。

JIT 对 PHP8 性能的影响:

PHP8-JIT

除此之外,还有一些类型系统和错误处理的改进、其他语法调整和改进以及新的类,接口和功能,详细的可以去 PHP 官网 查看

这里值得一提的是Opaque objects,用来代替 Curl、Gd、Sockets、OpenSSL、XMLWriter 和 XML 扩展的资源类型

//PHP7 var_dump(is_resource(curl_init())); // true //PHP8 var_dump(is_resource(curl_init())); // false var_dump(is_object(curl_init())); // true 
原文链接: https://mp.weixin.qq.com/s?__biz=MzAwOTgzNjY4MA==&mid=2247483847&idx=1&sn=8d7d08ebf018d64ecb6a11d0cb742020