PHP Reflection (Reflection) Usage instance
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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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 '); Get all Properties Print_r ($r->getproperties ()); /** * Get specified properties * Reflectionproperty::is_static * Reflectionproperty::is_public * reflectionproperty::is_protected * Reflectionproperty::is_private */ Print_r ($r->getproperties (reflectionproperty::is_private)); Get comments Print_r ($r->getproperty (' id ')->getdoccomment ()); Get method Print_r ($r->getmethods ()); |
The Reflectionextension class is used to obtain information about the extension
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
$re = new Reflectionextension (' Reflection '); Print_r ($re->getclasses ()); All classes of the extension Print_r ($re->getclassnames ()); Extend all class names $dom = new Reflectionextension (' MySQL '); Print_r ($dom->getconstants ());//Extended constant Print_r ($dom->getdependencies ());//the extension depends 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
?
1 2 3 4 5 |
$RF = new Reflectionfunction (' Array_merge '); foreach ($rf->getparameters () as $item) { Echo $item. Php_eol; } |
Reflectionmethod class users get information about methods
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st |
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 get information about an object
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 |
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.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 |
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 ()); 0 Print_r ($p->getname ()); V |
Reflectionproperty gets information about the properties of the class.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 |
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 ()); |
http://www.bkjia.com/PHPjc/1000099.html www.bkjia.com true http://www.bkjia.com/PHPjc/1000099.html techarticle PHP Reflection (Reflection) Use examples This article mainly introduces the use of PHP reflection (Reflection) examples, this article explains Reflectionclass, reflectionextension, Reflectionfunction, Reflec ...