cwola / attribute
PHP 属性(Cwola 库)。
v1.0.1
2022-07-10 04:32 UTC
Requires
- php: >=8.0.0
README
PHP 属性(Cwola 库)。
概述
为PHP添加"ReadableAttribute"和"WritableAttribute"。
需求
- PHP8.0+
安装
composer require cwola/attribute
使用方法
- 可读
<?php
use Cwola\Attribute\Readable;
class Foo {
use Readable;
/**
* @var string
*/
#[Readable]
protected string $protectedString = 'Protected';
}
class Bar extends Foo {
/**
* @var string
*/
protected string $override = 'OVER RIDE!!';
/**
* {@inheritDoc}
*/
private function __read(string $name): mixed {
return $this->override;
}
}
$foo = new Foo;
echo $foo->protectedString; // Protected
$foo->protectedString = 'modify'; // Error
$custom = new Bar;
echo $custom->protectedString; // OVER RIDE!!
echo $custom->override; // Error