它将包括属性钩子、JIT 改进,以及在不需要额外括号的情况下链式调用方法。这是一个大变化!
你们的项目开始支持8.4了嘛
属性钩子RFC
现代 PHP 历史上最大的变化之一:定义属性钩子的能力。
class BookViewModel
{
public function __construct(
private array $authors,
) {}
public string $credits {
get {
return implode(', ', array_map(
fn (Author $author) => $author->name,
$this->authors,
));
}
}
public Author $mainAuthor {
set (Author $mainAuthor) {
$this->authors[] = $mainAuthor;
$this->mainAuthor = $mainAuthor;
}
get => $this->mainAuthor;
}
}
属性钩子的目标是通过允许每个属性定义自己的 get 和 set 钩子,去除大量的 getter 和 setter。钩子是可选的,不必在特定属性上同时添加两个钩子。例如,只有 get 钩子的属性是虚拟属性。这应该是目前 PHP 8.4 最大的更新了,非常期待,又少写了好多代码 😂
interface HasAuthors
{
public string $credits { get; }
public Author $mainAuthor { get; set; }
}
142 天前