PHPMagicAnnotations
Release 0.8.3
PHP没有任何类型的本机注释(又称来自.NET World的属性),因此,如果您想实现自己的注释框架,请先考虑使用此框架并节省一些时间。
使用作曲家非常简单,只需运行以下命令:
$ composer require thomas-squall/php-magic-annotations
首先,您必须创建一个新课程。在此示例中,课程将称为mycustomantation
class MyCustomAnnotation
{
}然后,您必须从库扩展注释类
use PHPAnnotations Annotations Annotation ;
class MyCustomAnnotation extends Annotation
{
}在其中添加一些逻辑
use PHPAnnotations Annotations Annotation ;
class MyCustomAnnotation extends Annotation
{
private $ name ;
private $ surname ;
public function __constructor ( $ name , $ surname )
{
$ this -> name = $ name ;
$ this -> surname = $ surname ;
}
public function GetFullName ()
{
return " $ this -> name $ this -> surname " ;
}
}现在,我们美丽的注释已经准备好了!
创建一个用于测试注释的类
class MyTestClass
{
}并通过文档添加注释
/**
* @MyCustom(name = "Thomas", surname = "Cocchiara")
**/
class MyTestClass
{
}现在我们准备好测试了!
use use PHPAnnotations Reflection Reflector ;
$ myObject = new MyTestClass ();
$ reflector = new Reflector ( $ myObject );
echo $ reflector -> getClass ()-> getAnnotation ( " MyCustom " )-> GetFullName ();希望你们发现这个图书馆有用。
请分享并给我反馈:)
托马斯