>  기사  >  백엔드 개발  >  PHP의 다양한 리플렉션 클래스 사용 예 요약

PHP의 다양한 리플렉션 클래스 사용 예 요약

伊谢尔伦
伊谢尔伦원래의
2017-07-01 10:54:071176검색

이 글에서는 주로 PHP Reflection(Reflection)의 사용 예를 소개합니다. 이 글에서는 ReflectionClass, ReflectionExtension, ReflectionFunction, ReflectionMethod, ReflectionObject, ReflectionParameter 및 기타 클래스의 사용 예를 설명합니다. 필요한 친구들이 참고할 수 있습니다.

PHP Reflection이 사용됩니다. 클래스, 확장, 메소드, function, 객체, 매개변수, 속성 세부정보를 얻으려면
ReflectionClass 클래스는 속성, 메소드, 문서 주석 등 클래스 관련 정보를 얻습니다.

<?php
 
class Person {
  /**
   * For the sake of demonstration, we"re setting this private
   */
  private $_allowDynamicAttributes = false;
 
  /** type=primary_autoincrement */
  protected $id = 0;
 
  /** type=varchar length=255 null */
  protected $name;
 
  /** type=text null */
  protected $biography;
 
  public function getId()
  {
    return $this->id;
  }
  public function setId($v)
  {
    $this->id = $v;
  }
  public function getName()
  {
    return $this->name;
  }
  public function setName($v)
  {
    $this->name = $v;
  }
  public function getBiography()
  {
    return $this->biography;
  }
  public function setBiography($v)
  {
    $this->biography = $v;
  }
}
 
//导出类
ReflectionClass::export(&#39;Person&#39;);
 
$r = new ReflectionClass(&#39;Person&#39;);
 
//获取所有属性
print_r($r->getProperties());
 
/**
 * 获取指定属性
 * ReflectionProperty::IS_STATIC
 * ReflectionProperty::IS_PUBLIC
 * ReflectionProperty::IS_PROTECTED
 * ReflectionProperty::IS_PRIVATE
 */
print_r($r->getProperties(ReflectionProperty::IS_PRIVATE));
 
//获取注释
print_r($r->getProperty(&#39;id&#39;)->getDocComment());
 
//获取方法
print_r($r->getMethods());

ReflectionExtension 클래스는 확장 관련 정보를 얻는 데 사용됩니다

$re = new ReflectionExtension(&#39;Reflection&#39;);
print_r($re->getClasses()); //扩展的所有类
print_r($re->getClassNames()); //扩展所有类名
 
$dom = new ReflectionExtension(&#39;mysql&#39;);
print_r($dom->getConstants());//扩展常量
print_r($dom->getDependencies());//该扩展依赖
print_r($dom->getFunctions());//扩展方法
print_r($dom->getINIEntries());//扩展ini信息
print_r($dom->getName());//扩展名称
print_r($dom->getVersion());//扩展版本
print_r($dom->info());//扩展信息
print_r($dom->isPersistent());//是否是持久扩展
print_r($dom->isTemporary()); //是否是临时扩展

ReflectionFunction 클래스 사용자는 함수 관련 정보를 얻습니다.

$rf = new ReflectionFunction(&#39;array_merge&#39;);
 
foreach($rf->getParameters() as $item) {
  echo $item . PHP_EOL;
}

ReflectionMethod 클래스 사용자는 메소드 관련 정보를 얻습니다.

class Person {
 
  public $name;
 
  /**
   * get name of person
   */
  public function getName()
  {
    return $this->name;
  }
  public function setName($v)
  {
    $this->name = $v;
  }
}
 
$rm = new ReflectionMethod(&#39;Person&#39;, &#39;getName&#39;);
 
print_r($rm->isPublic());
print_r($rm->getDocComment());

ReflectionObject 클래스는 객체 관련 정보를 얻는 데 사용됩니다.

class Person {
 
  public $name;
 
  public function construct($name)
  {
    $this->name = $name;
  }
  
  public function getName()
  {
    return $this->name;
  }
  
  public function setName($v)
  {
    $this->name = $v;
  }
}
 
$a = new Person(&#39;a&#39;);
 
$ro = new ReflectionObject($a);
 
print_r($ro->getMethods());

ReflectionParameter는 함수나 메소드를 얻기 위해 매개변수에 대한 정보를 제공합니다.

class Person {
 
  public $name;
 
  public function construct($name)
  {
    $this->name = $name;
  }
 
  public function getName()
  {
    return $this->name;
  }
 
  public function setName($v)
  {
    $this->name = $v;
  }
}
 
$p = new ReflectionParameter(array(&#39;Person&#39;, &#39;setName&#39;), 0);
 
print_r($p->getPosition()); //0
print_r($p->getName()); //v

ReflectionProperty 클래스 속성에 대한 정보를 가져옵니다.

아아아아

위 내용은 PHP의 다양한 리플렉션 클래스 사용 예 요약의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.