This article mainly introduces the PHP reflection (Reflection) Use example, this article explains Reflectionclass, reflectionextension, Reflectionfunction, Reflectionmethod, Reflectionobject, Reflectionparameter and other classes of use examples, the need for friends can refer to the following
PHP reflection is used to get details about classes, extensions, methods, functions, objects, parameters, properties.
The Reflectionclass class gets information about the class, such as getting properties, methods, document comments, and so on.
<?php class Person {/** * For the sake of demonstration, we ' re setting this private */private $_allowdynamicatt Ributes = 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; The Public Function setId ($v) {$this->id = $v; } public Function GetName () {return $this->name; The Public Function SetName ($v) {$this->name = $v; } public Function getbiography () {return $this->biography; The Public Function setbiography ($v) {$this->biography = $v; }}//Export class Reflectionclass::export (' person '); $r = new Reflectionclass (' person '); Get all Attributes Print_r ($r->getproperties ()); /** * Get Specified Property * reflectionproperty::is_static * reflectionproperty::is_public * reflectionproperty::is_protected * Reflectionproperty::is_private */print_r ($r->getproperties (reflectionproperty::is_private)); Get Comment pRint_r ($r->getproperty (' id ')->getdoccomment ()); Get method Print_r ($r->getmethods ());
The Reflectionextension class is used to obtain information about the extension
$re = new Reflectionextension (' Reflection ');p rint_r ($re->getclasses ()); All classes of Extended Print_r ($re->getclassnames ()); Extend all class names $dom = new Reflectionextension (' MySQL ');p rint_r ($dom->getconstants ());//Extended constant Print_r ($dom Getdependencies ());//The extension relies on Print_r ($dom->getfunctions ());//extension method Print_r ($dom->getinientries ());// Extended INI Information print_r ($dom->getname ()),//Extension 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
Reflectionfunction class users get information about functions
$RF = new Reflectionfunction (' Array_merge '); foreach ($rf->getparameters () as $item) { echo $item. Php_eol;}
Reflectionmethod class users get information about methods
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 ());p Rint_r ($rm->getdoccomment ());
The Reflectionobject class is used to get information about an object
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 gets information about a function or method parameter.
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 (the ' person ', ' SetName '), 0); Print_r ($p->getposition ()); 0print_r ($p->getname ()); V
Reflectionproperty gets information about the properties of the 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 ());