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 ());