首页 / 教程资源

php 8.2 新特性,快速了解

发布时间:2023-05-11 10:52:53

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); // 1var_dump(Foo::CONSTANT); // Error

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

看完本文有收获?点赞、分享是最大的支持!

相关推荐