alkaupp / readonly
提供只读公共属性的特质
1.1
2021-02-04 17:18 UTC
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-05 01:05:41 UTC
README
composer require alkaupp/readonly
介绍
你想要创建数据对象但不想在代码中维护getter方法吗?创建具有公共属性但这些属性只允许读操作的类可能是合理的。
这个库通过提供一个允许您使用私有属性编写数据类并且仍然可以访问它们的Readonly特质来解决该问题。
该特质防止对私有属性的重赋值。要获得完全不可变,您将必须使用不可变对象。
<?php declare(strict_types=1); namespace Tests\Alkaupp\Readonly; use Alkaupp\Readonly\Readonly; use DateTime; /** * @property string $firstName * @property string $lastName * @property DateTime $birthday */ final class Example { use Readonly; private $firstName; private $lastName; private $birthday; public function __construct(string $firstName, string $lastName, DateTime $birthday) { $this->firstName = $firstName; $this->lastName = $lastName; $this->birthday = $birthday; } }