只读类
<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
看完本文有收获?点赞、分享是最大的支持!