期待已久的PHP8.0正式版发布,引入重磅功能JIT

2020年11月27日22:50:00 发表评论 154 次浏览

11月27日正好是每年的感恩节,正式版本的php8.0现在正式开放供下载。
期待已久的PHP8.0正式版发布,引入重磅功能JIT
Php8.0引入了jit编译器特性,同时添加了各种新的语言特性,如命名参数、联邦类型、注释、构造属性提升、匹配表达式、空时安全操作符以及类型系统的改进、错误处理和一致性。

几大新特性:

重磅新功能之及时编译JIT

php8.0版本最重要的特点是jit。jit的引入过程也是不合时宜的,它更新了php 5和7个主要版本(php 6项目流产),直到去年3月,通过投票最终确认它将进入第8阶段。

PHP 8 引入了两个 JIT 编译引擎。跟踪 JIT 是其中最有前途的,在合成基准测试上显示的性能提高约 3 倍,在某些特定长时间运行的应用程序上显示 1.5-2 倍的性能。典型的应用程序性能与 PHP 7.4 平起平坐。
对 PHP 8 性能的相对 JIT 贡献
期待已久的PHP8.0正式版发布,引入重磅功能JIT

构造属性升级

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

匹配表达式

//php7
  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

空安全运算符R

//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 字符串到数字比较

//PHP7
0 == 'foobar' // true
//PHP8
0 == 'foobar' // false
阿修罗

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: