[Solid PHP Foundation] PHP reflection mechanism, solid php reflection mechanism
Address of this Article
Sharing outline:
1. Introduction
2. Example
2.1 create a Persion class
2.2 reflection process
2.3 use after reflection
1. Introduction
-- PHP5 adds a new function: Reflection. This function enables phper to reverse-engineer class, interface, function, method and extension. With PHP code, you can obtain all the information of an object and interact with it.
-- What is reflection?
It refers to the Extension Analysis of PHP programs in the PHP running state, exporting or extracting detailed information about classes, methods, attributes, parameters, and so on, including comments. This kind of dynamic information and the function of dynamically calling an object is called a reflection API. Reflection is an API used to manipulate the object-oriented model. It is very powerful and can help us build complex and scalable applications.
Its purpose is to automatically load plug-ins, generate documents automatically, and even expand the PHP language.
The php reflection api consists of several classes that can help us access the metadata of a program or interact with related annotations. With reflection, we can obtain methods implemented by the class, create an instance of the class (different from creating with new), call a method (also different from conventional call), and pass parameters, static Methods of the dynamic call class.
Reflection APIs are extensions of php built-in oop technology, including some classes, exceptions, and interfaces. They can be used to help us analyze other classes, interfaces, methods, attributes, methods, and extensions. These oop extensions are called reflection.
Through ReflectionClass, we can get the following information about the Person class:
1) constant Contants
2) Property Names
3) Method Names static
4) attribute Static Properties
5) Namespace
6) whether the Person class is final or abstract
2. Example
Create a Person class and use ReflectionClass to reflect it 2.1) [Create a Persion class] 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; publicfunction 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 ;}}Persion
2.2) [reflection process]
Next, you only need to pass the class name "Person" to ReflectionClass:
1 $ class = new ReflectionClass ('person '); // create the reflection class of the Person class 2 $ instance = $ class-> newInstanceArgs ($ args ); // It is equivalent to instantiating the Person class
2.3) [Use after reflection]
2.3.1) Obtain Properties)
$ Properties = $ class-> getProperties (); foreach ($ properties as $ property) {echo $ property-> getName (). "\ n";} // output: // _ allowDynamicAttributes // id // name // biography
By default, ReflectionClass obtains all attributes, and private and protected attributes can also be obtained. If you only want to get the private property, you need to pass an additional parameter:
$ Private_properties = $ class-> getProperties (ReflectionProperty: IS_PRIVATE );
List of available parameters:
ReflectionProperty: IS_STATIC
ReflectionProperty: IS_PUBLIC
ReflectionProperty: IS_PROTECTED
ReflectionProperty: IS_PRIVATE
If you want to obtain both the public and private attributes, write: ReflectionProperty: IS_PUBLIC | ReflectionProperty: IS_PROTECTED.
You can get the property name through $ property-> getName.
2.3.2) [obtain comments]
You can use getDocComment to get comments to the property.
1 foreach($propertiesas$property) { 2 if($property->isProtected()) { 3 $docblock = $property->getDocComment(); 4 preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches); 5 echo$matches[1]."\n"; 6 } 7 } 8 // Output: 9 // primary_autoincrement10 // varchar11 // text
2.3.3) [method for retrieving classes]
Method: getMethods () is used to obtain all the methods of the class.
2.3.4) [execution method]
1 $ instance-> getBiography (); // execute the getBiography2 method in Person // or: 3 $ ec = $ class-> getmethod ('getname '); // get the getName Method 4 $ ec-> invoke ($ instance) in the Person class; // execute the getName Method