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 ();希望你們發現這個圖書館有用。
請分享並給我反饋:)
托馬斯