美文网首页
PHP 8 发布了

PHP 8 发布了

作者: 程序员小乔 | 来源:发表于2020-11-29 00:15 被阅读0次
    PHP 8

    PHP 8.0 是 PHP 语言的一个主版本更新。

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

    1. 命名参数 RFC

    在php7 中:

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

    在php8 中:

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

    2. 注解 RFC

    在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 原生语法来使用结构化的元数据,而非 PHPDoc 声明。

    3. 构造器属性提升 RFC 文档

    在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,
      ) {}
    }
    
    
    • 更少的样板代码来定义并初始化属性。

    4. 联合类型 RFC 文档

    在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
    
    
    • 相对于以前的 PHPDoc 声明类型的组合, 现在可以用原生支持的联合类型声明取而代之,可在实际运行中验证。

    5. Match 表达式 RFC 文档

    在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
    
    

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

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

    6. Nullsafe 运算符 RFC

    在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 运算符链式调用,而不需要条件检查 null。 如果链条中的一个元素失败了,整个链条会中止并认定为 Null。

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

    在php7 中:

    0 == 'foobar' // true
    
    

    在php8 中:

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

    8. 内部函数类型错误的一致性。 RFC

    在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 级异常。

    9. 即时编译

    PHP 8 引入了两个即时编译引擎。 Tracing JIT 在两个中更有潜力,它在综合基准测试中显示了三倍的性能, 并在某些长时间运行的程序中显示了 1.5-2 倍的性能改进。 典型的应用性能则和 PHP 7.4 不相上下。

    关于 JIT 对 PHP 8 性能的贡献

    image

    10. 类型系统与错误处理的改进

    • 算术/位运算符更严格的类型检测 RFC
    • Abstract trait 方法的验证 RFC
    • 确保魔术方法签名正确 RFC
    • PHP 引擎 warning 警告的重新分类 RFC
    • 不兼容的方法签名导致 Fatal 错误 RFC
    • 操作符 @ 不再抑制 fatal 错误。
    • 私有方法继承 RFC
    • Mixed 类型 RFC
    • Static 返回类型 RFC
    • 内部函数的类型 Email thread
    • 扩展 CurlGdSocketsOpenSSLXMLWriterXML 以 Opaque 对象替换 resource。

    11. 其他语法调整和改进

    • 允许参数列表中的末尾逗号 RFC、 闭包 use 列表中的末尾逗号 RFC
    • 无捕获的 catche RFC
    • 变量语法的调整 RFC
    • Namespace 名称作为单个 token RFC
    • 现在 throw 是一个表达式 RFC
    • 允许对象的 ::class RFC

    12. 新的类、接口、函数

    13. 几个弃用

    在 PHP 7. * 的开发期间,添加了几个弃用版本,这些弃用已于 PHP 8 最终确定。

    PHP 7.2 中的弃用

    PHP 7.3 中的弃用

    PHP 7.4 中的弃用

    总结:以上就是这篇文章的全部内容了。

    相关文章

      网友评论

          本文标题:PHP 8 发布了

          本文链接:https://www.haomeiwen.com/subject/pashwktx.html