PHP Reflection instance

Source: Internet
Author: User
This article describes the use cases of PHP Reflection (Reflection). This article describes the use cases of ReflectionClass, ReflectionExtension, ReflectionFunction, ReflectionMethod, ReflectionObject, ReflectionParameter, and other classes, for more information, see PHP Reflection to obtain detailed information about classes, extensions, methods, functions, objects, parameters, and attributes.
The ReflectionClass class obtains information about a class, such as attributes, methods, and document comments.

<? 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 ;}// export class ReflectionClass: export ('person '); $ r = new ReflectionClass ('person '); // Obtain all attributes print_r ($ r-> getProperties ();/*** obtain the specified attribute * ReflectionProperty: IS_STATIC * ReflectionProperty: IS_PUBLIC * ReflectionProperty :: IS_PROTECTED * ReflectionProperty: IS_PRIVATE */print_r ($ r-> getProperties (ReflectionProperty: IS_PRIVATE); // Obtain the annotation print_r ($ r-> getProperty ('id ') -> getDocComment (); // Obtain the method print_r ($ r-> getMethods ());

The ReflectionExtension class is used to obtain extended information.

$ Re = new ReflectionExtension ('Reflection '); print_r ($ re-> getClasses (); // All extended classes print_r ($ re-> getClassNames ()); // extend all class names $ dom = new ReflectionExtension ('mysql'); print_r ($ dom-> getConstants ()); // extended constant print_r ($ dom-> getDependencies (); // This extension depends on print_r ($ dom-> getFunctions ()); // extended method print_r ($ dom-> getINIEntries (); // extended ini information print_r ($ dom-> getName ()); // extended name print_r ($ dom-> getVersion (); // extended version print_r ($ dom-> info ()); // extended information print_r ($ dom-> isPersistent (); // whether it is a persistent extension print_r ($ dom-> isTemporary (); // whether it is a temporary extension

The ReflectionFunction user obtains function-related information.

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

ReflectionMethod users obtain method-related information

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

The ReflectionObject class is used to obtain object-related information.

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('a'); $ro = new ReflectionObject($a); print_r($ro->getMethods());

ReflectionParameter obtains information about function or method parameters.

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('Person', 'setName'), 0); print_r($p->getPosition()); //0print_r($p->getName()); //v

ReflectionProperty obtains information about the attributes of a class.

Class Person {/** test */public $ name; public function _ construct ($ name) {$ this-> name = $ name;} public function getName () {return $ this-> name;} public function setName ($ v) {$ this-> name = $ v; }}$ p = new ReflectionProperty ('person ', 'name'); print_r ($ p-> getDocComment ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.