自行實現PHP代碼引文特性

來源:互聯網
上載者:User
自行實現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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.