PHP8 正式版发布了

2020年11月26日 214点热度 0人点赞 0条评论

PHP8.0 是 PHP 的一个大版本更新。它包含许多新功能和优化,包括命名参数,联合类型,属性,构造函数属性提升,匹配表达式,nullsafe 运算符,JIT,以及类型系统的改进,错误处理和一致性。


01

命名参数


PHP7

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


PHP8

htmlspecialchars($string, double_encode: false);


  • 仅指定必需的参数,跳过可选的参数。

  • 参数是 order-independent 和 self-documented。


02

属性


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) { /* ... */ }}

现在,您可以使用具有PHP native 语法的结构化元数据来代替 PHPDoc 注释。

03

构造函数属性提升

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,  ) {}}

更少的 boilerplate 代码来定义和初始化属性。

04

联合类型

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

可以使用在运行时验证的 native 联合类型声明来代替类型组合的 PHPDoc 注释。

05

匹配表达式

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

新的匹配表达式类似于switch,并具有以下功能:

  • Match 是一个表达式,表示其结果可以存储在变量中或返回。

  • Match 分支仅支持单行表达式,不需要break,statement。

  • Match 做严格的比较。

06

nullsafe 运算符

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;

现在,您可以使用带有新的 nullsafe 运算符的调用链来代替空检查条件。当对链中一个元素的求值失败时,整个链的执行将中止,并且整个链的求值为空。

07

Saner 字符串与数字的比较

PHP7

0 == 'foobar' // true

PHP8

0 == 'foobar' // false

与数字字符串进行比较时,PHP8 使用数字比较。否则,它将数字转换为字符串并使用字符串比较。

08

内部函数的一致类型错误

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

现在,如果参数验证失败,大多数内部函数将引发 Error 异常。

09

Just-In-Time 编译

PHP8 引入了两个 JIT 编译引擎。Tracing JIT 是两者中最有希望的,它在综合基准测试中的性能提高了大约 3 倍,在某些特定的长期运行的应用程序中提高了 1.5–2 倍。典型的应用程序性能与 PHP7.4 相当。

JIT 对PHP8 性能的贡献

图片

10

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

  • 对 arithmetic/bitwise operators 进行更严格的类型检查

  • 抽象特征方法验证

  • 正确的魔术方法签名

  • Reclassified engine warnings

  • 不兼容的方法签名的致命错误

  • @ 运算符不再使致命错误失效

  • 用私有方法继承

  • Mixed type

  • 静态返回类型

  • 内部函数的类型

  • Opaque objects instead of resources for Curl, Gd, Sockets, OpenSSL, XMLWriter, and XMLextensions

11

其他语法调整和改进

  • 在参数列表中允许逗号结尾和闭包使用列表

  • Non-capturing catches

  • 可变语法调整

  • 将命名空间名称视为单个令牌

  • Throw is now an expression

  • Allow ::class on objects

12

新的类,接口和函数

  • Weak Map class

  • Stringable interface

  • str_contains(), str_starts_with(), str_ends_with()

  • fdiv()

  • get_debug_type()

  • get_resource_id()

  • token_get_all() object implementation


图片

参考资料:

https://www.php.net/releases/8.0/en.php

https://www.php.net/ChangeLog-8.php

6910PHP8 正式版发布了

root

这个人很懒,什么都没留下

文章评论