您的当前位置:首页>全部文章>文章详情

PHP8出来也已经有一段时间了 说说PHP8的新特性

发表于:2022-04-07 09:36:25浏览:1005次TAG: #PHP8 #PHP8新特性

PHP8出来也已经有一段时间了,但是真正使用到项目中的公司却还不多,虽然功能新增了很多,但是因为作为最新版本的php,是不可能直接应用到项目中的,只有经过真操实弹的项目检测,才会慢慢推广迭代,但是作为最新版本的PHP8,它的新特性还是必须要知了解的。
PHP8官方网站:https://www.php.net/releases/8.0/en.php

1、命名参数
官方英文:Named arguments
传参的时候,可以跳过可选参数,这在之前版本中不行

<?php
//php7
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);

//php8
htmlspecialchars($string, double_encode: false);

2、属性注解
官方英文:Attributes
可以使用PHP原生语法来使用结构化元数据,而非PHPDoc申明

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

3、构造器属性的提升
官方英文:Constructor property promotion
可以使用更简便的方法初始化属性

<?php
//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、联合类型
官方英文:Union types
老版本申明类型单一,php8可以申明多种类型

<?php
//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

5、match表达式
官方英文:Match expression
switch表达式的简化

<?php
//php7
switch (8.0) {
  case '8.0':
    $result = "Oh no!";
    break;
  case 8.0:
    $result = "This is what I expected";
    break;
}

//php8
echo match (8.0) {
  '8.0' => "Oh no!",
  8.0 => "This is what I expected",
};

6、nullsafe运算符
官方英文:Nullsafe operator
可以使用nullsafe运算符链式调用,而不需要检测null,如果其中一个不满足,直接中断并返回null

<?php
//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;

7、字符串数字弱类型比较优化
官方英文:Saner string to number comparisons
字符串和数字的比较更加的严格

<?php
//php7
0 == 'foobar' // true
//php8
0 == 'foobar' // false

8、函数错误一致性
官方英文:Consistent type errors for internal functions
以前错误类型会分notice、warning、error,现在大多数位TypeError

<?php
//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

9、JIT优化
官方英文:Relative JIT contribution to PHP 8 performance
这个改动被鸟哥成为php8中最重要的改动,php8中引入了两个即时编译引擎,Tracing JIT在两个中潜力更大,显示了三倍的性能,某些长时间运行的程序中也显示了1.5-2倍的性能改进

10、其他改进
操作符@将不再抑制fatal类型错误
assert()不再支持执行代码,减少了安全漏洞
create_function()函数彻底被移除
因为libxml的依赖最低2.9.0起,所以XXE漏洞彻底可以消失了
Phar中元信息不再进行自动反序列化了,phar://触发反序列化的姿势也告别了
parse_str()必须传入第二个参数了,少了一种全局变量覆盖的方法