php 8.2 新特性,快速了解

PHP一种流行的通用脚本语言,特别适合 Web 开发。PHP 快速、灵活且实用,为从您的博客到世界上最流行的网站的一切提供支持。

PHP 8.2 是 PHP 语言的重大更新。它包含许多新功能,包括只读类、作为独立类型的 null、false 和 true、弃用的动态属性、性能改进等。

只读类

<8.2

class BlogData {
    public readonly string $title;

    public readonly Status $status;

    public function __construct(string $title, Status $status){
        $this->title = $title;
        $this->status = $status;
    }
}

=8.2

readonly class BlogData{
    public string $title;

    public Status $status;

    public function __construct(string $title, Status $status){
        $this->title = $title;
        $this->status = $status;
    }
}
 
 

析取范式 (DNF) 类型

<8.2

class Foo
{
    public function bar(mixed $entity)
{
        if ((($entity instanceof A) && ($entity instanceof B)) || ($entity === null)) {
            return $entity;
        }


        throw new Exception('Invalid entity');
    }
}

=8.2

class Foo {
    public function bar((A&B)|null $entity) {
        return $entity;
    }
}
 
 


允许null、false和true作为独立类型

<8.2

class Falsy {
    public function almostFalse(): bool { /* ... */ *}


    public function almostTrue(): bool { /* ... */ *}


    public function almostNull(): string|null { /* ... */ *}
}

=8.2

class Falsy{
    public function alwaysFalse(): false { /* ... */ *}


    public function alwaysTrue(): true { /* ... */ *}


    public function alwaysNull(): null { /* ... */ *}
}

特征中的常量

=8.2

trait Foo{
    public const CONSTANT = 1;
}


class Bar {
    use Foo;
}


var_dump(Bar::CONSTANT); // 1
var_dump(Foo::CONSTANT); // Error

更多新特性请看官方官网:https://www.php.net/releases/8.2/en.php

95d5c42488b068f403abb0ddd7c07bca.jpeg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值