PHP RFC: Typed Properties 2.0 通过,PHP 7.4 新特性 类属性的类型声明

随着标量类型和返回类型的引入,PHP 7 大大增强了 PHP 类型系统的功能。 但是,目前无法为类属性声明类型,从而迫使开发人员使用 getter 和 setter 方法来强制执行类型契约。 这要求了不必要的样板,使得使用不那么符合人体工程学的方式,并且对性能有不好影响。 此 RFC 通过引入对类属性类型声明的支持来解决该问题。

此前,

class User {
    /** @var int $id */
    private $id;
    /** @var string $name */
    private $name;

    public function __construct(int $id, string $name) {
        $this->id = $id;
        $this->name = $name;
    }

    public function getId(): int {
        return $this->id;
    }
    public function setId(int $id): void {
        $this->id = $id;
    }

    public function getName(): string {
        return $this->name;
    }
    public function setName(string $name): void {
        $this->name = $name;
    }
}

现在,

class User {
    public int $id;
    public string $name;

    public function __construct(int $id, string $name) {
        $this->id = $id;
        $this->name = $name;
    }
}

详情: https://wiki.php.net/rfc/typed_properties_...

癞蛤蟆想吃炖大鹅
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!