创意电子

标题: PHP 8.0正式发布:支持JIT编译器,性能提升高达3倍 [打印本页]

作者: InfoQ    时间: 2020-11-27 16:31
标题: PHP 8.0正式发布:支持JIT编译器,性能提升高达3倍
美国时间11月26日,PHP团队宣布PHP 8.0正式GA。PHP 8.0是PHP语言的最新主要版本,带来了许多新特性和优化,包括命名参数(named arguments)、联合类型(union types)、属性(attributes)、构造器属性提升(constructor property promotion)、Match表达式、nullsafe运算符、JIT,以及针对类型体系、错误处理和同等性的诸多改进。

                               
登录/注册后可看大图

PHP 8.0.0 下载地址:
https://www.php.net/downloads
下文将对新版本的紧张亮点做简单介绍:
命名参数

https://wiki.php.net/rfc/named_params
PHP 7
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);PHP 8
htmlspecialchars($string, double_encode: false);
属性

如今,开发者可以使用基于PHP原生语法的结构化元数据来代替PHPDoc注解。
https://wiki.php.net/rfc/attributes_v2
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) { /* ... */ }}构造器属性提升

新版本界说和初始化属性所用的样板代码更少。
https://wiki.php.net/rfc/constructor_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)

Union Types 支持接收多个差别类型的值,而不是单个类型。目前PHP已经支持两种特别的联合类型:
对于类型组合,可以使用在运行时经过验证的原生联合类型声明来代替PHPDoc注解。
https://wiki.php.net/rfc/union_types_v2
支持联合类型之后,将会允许将更多类型信息从 phpdoc 迁移至函数签名。可以说,泛型之后,联合类型是目前类型声明体系中最大的突破口。
PHP 7
class Number {  /** @var int|float */  private $number;  /**   * @param float|int $number   */  public function __construct($number) {   $this->number = $number; }}new Number('NaN'); // OPHP 8
class Number {  public function __construct(   private int|float $number  ) {}}new Number('NaN'); // TypeErrorMatch表达式

新的match很像switch,并具有以下特性:
https://wiki.php.net/rfc/match_expression_v2
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 expectedNullsafe运算符

如今,开发者可以使用带有新的nullsafe运算符的调用链来代替null check。当对链中一个元素的求值失败时,整个链的实验将中止,并且整个链的求值为null。
https://wiki.php.net/rfc/nullsafe_operator
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;字符串与数字的判定更合理

使用==和其他非严格比力运算符对字符串和数字之间做比力时,原本的做法是将字符串逼迫转换为数字,然后对整数或浮点数进行比力。这会导致许多令人惊奇的比力结果,其中最值得注意的是 0 == "foobar" 返回true。
在新版本中,仅在字符串实际为数字时才使用数字比力,否则将数字转换为字符串,并实验字符串比力。
https://wiki.php.net/rfc/string_to_number_comparison
PHP 7
0 == 'foobar' // truePHP 8
0 == 'foobar' // false内部函数的类型错误同等

在新版本中,如果参数验证失败,大多数内部函数将抛出Error异常。
https://wiki.php.net/rfc/consistent_type_errors
PHP 7
strlen([]); // Warning: strlen() expects parameter 1 to be string, array givenarray_chunk([], -1); // Warning: array_chunk(): Size parameter expected to be greater than 0PHP 8
strlen([]); // TypeError: strlen(): Argument #1 ($str) must be of type string, array givenarray_chunk([], -1); // ValueError: array_chunk(): Argument #2 ($length) must be greater than 0JIT编译

PHP 8引入了两个JIT编译引擎。Tracing JIT的表现最出色,它在综合基准测试中的性能进步到大约3倍,在某些特定的传统应用步调中进步到1.5–2倍。典范的应用步调性能与PHP 7.4相称。
JIT对PHP 8性能的贡献

                               
登录/注册后可看大图

类型体系和错误处理方面的改进

其他语法调整和改进

新的类、接口和函数

下载

要下载PHP 8的源代码,请访问下载页面(https://www.php.net/downloads)。Windows二进制文件位于Windows版PHP网站(http://windows.php.net/download)。更改列表位于ChangeLog(http://www.php.net/ChangeLog-8.php)。
PHP手册中提供了迁移指南(https://www.php.net/manual/en/migration80.php)。请查阅它以获取新特性细节和向后不兼容更改的详细列表。
原文链接:https://www.php.net/releases/8.0/en.php
延伸阅读:
关注我并转发此篇文章,私信我“领取资料”,即可免费得到InfoQ价值4999元迷你书,点击文末「了解更多」,即可移步InfoQ官网,获取最新资讯~
作者: 子非    时间: 2020-11-28 00:22
转发了
作者: 雕牌洗衣粉    时间: 2020-11-28 04:11
转发了
作者: 書童9527    时间: 2020-11-28 08:59
转发了
作者: 流星下的愿    时间: 2020-11-28 09:11
if($a==0) {echo 'hi'}else{echo 'no'}php8实际输出了no。。。很惊讶




欢迎光临 创意电子 (https://www.wxcydz.cc/) Powered by Discuz! X3.4