koriym/attributes
最新稳定版本:1.0.7
Composer 安装命令:
composer require koriym/attributes
包简介
An annotation/attribute reader
关键字:
README 文档
README
A koriym/attributes dual reader implements doctrine/annotation Reader interface
in order to read both doctrine/annotation and PHP 8 attributes.
Doctrine annotations are different by design than PHP core one. Not all attributes can be read by this reader (ex. parameters), and not all doctrine/annotations can be read by this reader either. (ex. nested annotations)
However, This reader help you to code forward compatible that supports both PHP 7.x annotations and 8.x attributes in certain senario.
Installation
composer require koriym/attributes
Usage
Create the reader instance.
use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\Reader; use Koriym\Attributes\DualReader; use Koriym\Attributes\AttributeReader; $reader = new DualReader( new AnnotationReader(), new AttributeReader() ); assert($reader instanceof Reader);
The reader can read both annotations and attributes.
Compatible Annotation
Existing doctrine annotations can be changed into annotations that work for both doctrine annotation and PHP8 attributes.
Add #[Attribute] attribute.
use Attribute;
/** @Annotation */
+#[Attribute]
final class Foo
{
}
Then add constructor when annotation has properties.
Following example works with both PHP8 attribute and doctrine/annotations.
use Attribute; +use Doctrine\Common\Annotations\NamedArgumentConstructor; /** * @Annotation * @Target("METHOD") +* @NamedArgumentConstructor */ +#[Attribute(Attribute::TARGET_METHOD)] final class Foo { public string $bar; public int $baz; + public function __construct(string $bar = '', int $baz = 0) + { + $this->bar = $bar; + $this->baz = $baz; + } }
See more about annotation compatible attribute at Constructors with Named Parameters
统计信息
- 总下载量: 3.14M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 44
- 点击次数: 1
- 依赖项目数: 14
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-17