首页 小组 问答 话题 好文 素材 用户 唠叨 我的社区

[交流]PHP 8.4 将于 2024 年 11 月 21 日发布

道亮_(:з」∠)_Lv.1管理员
2024-06-02 22:28:09
0
82

它将包括属性钩子、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 天前

签名 : 不交僧道,便是好人。   82       0
评论
站长交流