PHP 코드 주석 기능을 직접 구현해 보세요.

WBOY
풀어 주다: 2016-08-08 09:24:20
원래의
2028명이 탐색했습니다.

PHP Annotation

지금까지 PHP의 Reflection 기능은 Annotation을 지원하지 않았지만 기본 문서 주석 내용인 ReflectionMethod::getDocComment() 획득을 지원할 수 있습니다(5.1.0부터). PHP의 반영은 실제로 매우 강력합니다. 한 단계 더 나아가 문서 주석에서 관련 주석을 구문 분석하면 됩니다.

AppServer.io는 주석을 지원하는 언어 라이브러리를 제공합니다. PHP의 Tokenizer 기능은 주석 코드를 구문 분석하는 데에도 사용됩니다. 구체적인 원리는 자세히 설명되어 있지 않습니다. 관심이 있는 경우 코드를 직접 읽어 보세요.
https://github.com/appserver-io/lang

주석에 대한 지침은 http://appserver.io/get-started/documentation/annotations.html을 참조하세요.

데모 코드는 다음과 같이 발췌합니다.

<code><?php

namespace Namespace\Module;

use AppserverIo\Appserver\Lang\Reflection\ReflectionClass;
use AppserverIo\Appserver\Lang\Reflection\ReflectionAnnotation;

class Route extends ReflectionAnnotation
{

  /**
   * Returns the value of the name attribute.
   *
   * @return string The annotations name attribute
   */
  public function getPattern()
  {
    return $this->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();</code>
로그인 후 복사

이 기능을 통해 메소드의 URL 라우팅 모드/index/index를 주석으로 지정할 수 있습니다

이상 내용의 측면을 포함하여 PHP 코드 주석 기능의 자체 구현을 소개했습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!