自行實現PHP代碼註解特性
PHP 註解
到目前為止,PHP的反射特性中是不支援註解Annotation的,但是可以支援基本的文檔注釋內容的擷取 ReflectionMethod::getDocComment() - 從5.1.0開始 。PHP的反射其實已經挺強大的了,只要再進一步,解析文檔注釋中的相關註解內容即可。
AppServer.io 提供了一個lang庫,實現了對註解的支援。其中還運用了PHP的Tokenizer特性來解析註解代碼,具體原理不詳述,有興趣自行閱讀代碼。
https://github.com/appserver-io/lang
其關於註解的說明見:http://appserver.io/get-started/documentation/annotations.html
在此摘錄其示範代碼如下:
values['pattern']; }}class IndexController{ /** * Default action implementation. * * @return void * @Route(pattern="/index/index") */ public function indexAction() { // do something here }}// create a reflection class to load the methods annotation $reflectionClass = new ReflectionClass('IndexController');$reflectionMethod = $reflectionClass->getMethod('indexAction');$reflectionAnnotation = $reflectionMethod->getAnnotation('Route');$pattern = $reflectionAnnotation->newInstance()->getPattern();
通過這種特性,可以實現註解的方式指定方法的url路由模式 /index/index