全网最全的 php8 新特性

目录

一、 官方网站

二、命名参数

三、属性注解

四、构造器属性的提升

五、联合类型

六、match表达式

七、nullsafe运算符

八、字符串数字弱类型比较优化

九、函数错误一致性

十、JIT优化

十一、其他改进


一、 官方网站

二、命名参数

  • 官方英文:Named arguments
  • 传参的时候,可以跳过可选参数,这在之前版本中不行
<?php
//php7
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);

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

三、属性注解

  • 官方英文: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) { /* ... */ }
}

四、构造器属性的提升

  • 官方英文: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,
  ) {}
}

五、联合类型

  • 官方英文: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

六、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",
};

七、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;

八、字符串数字弱类型比较优化

  • 官方英文:Saner string to number comparisons
  • 字符串和数字的比较更加的严格
<?php
//php7
0 == 'foobar' // true

//php8
0 == 'foobar' // false

九、函数错误一致性

  • 官方英文: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

十、JIT优化

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

十一、其他改进

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客飞兔

你的支持是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值