The PHP5 has a complete reflection API that adds the ability to reverse engineer classes, interfaces, functions, methods, and extensions.
What is reflection?
It refers to the PHP running state, extending the parsing PHP program, exporting or extracting details about classes, methods, properties, parameters, etc., including comments. This dynamic acquisition of information and the ability to dynamically invoke an object's methods is called the Reflection API. Reflection is an API that manipulates the meta-model of an object-oriented paradigm and is powerful in helping us to build complex, scalable applications.
The purpose is to automatically load plugins, automatically generate documents, and even to augment the PHP language.
The PHP reflection API consists of several classes that help us to access the program's metadata or to interact with the related annotations. With reflection we can obtain methods such as class implementations, create an instance of a class (unlike the one created with new), Invoke a method (also different from a regular call), pass a parameter, and invoke the static method of the class dynamically.
The Reflection API is a PHP built-in OOP technology extension that includes classes, exceptions, and interfaces that are used in combination to help us analyze other classes, interfaces, methods, properties, methods, and extensions. These OOP extensions are known as reflections.
Usually we use more of the Reflectionclass class and the Reflectionmethod class , for example:
<?PHP02classPerson {03 04/**05 * For the sake of demonstration, we ' re setting this private06*/07Private $_allowdynamicattributes=false;08 09/**10 * type=primary_autoincrement11*/12protected $id= 0;13 14/**15 * Type=varchar length=255 null16*/17protected $name;18 19/**20 * Type=text null21*/22protected $biography;23 24 Public functiongetId () {25return $this-ID;26 }27 28 Public functionSetId ($v) {29$this->id =$v;30 }31 32 Public functionGetName () {33return $this-name;34 }35 36 Public functionSetName ($v) {37$this->name =$v;38 }39 40 Public functiongetbiography () {41return $this-biography;42 }43 44 Public functionSetbiography ($v) {45$this->biography =$v;46 }47}
First, through Reflectionclass, we can get the following information of the person class:
- Constant contants
- Property Names
- Method names static
- Property Static Properties
- Namespace Namespace
- Whether the person class is final or abstract
- Does the person class have a method
Next reflect it, as long as the class name "person" is passed to Reflectionclass, you can:
1 $classnew/2 $instance $class Newinstanceargs ($args// equivalent to instantiating the person class
1) Get Attributes (properties):
1$properties=$class-getProperties ();2foreach($properties as $property) {3Echo $property->getname (). "\ n";4 }5//Output:6//_allowdynamicattributes7//ID8//name9//biography
By default, Reflectionclass will get all of the properties, private and protected can also. If you want to get only the private property, you need to pass an additional parameter:
1 $private _properties$class->getproperties (reflectionproperty::is_private);
List of available parameters:
- Reflectionproperty::is_static
- Reflectionproperty::is_public
- reflectionproperty::is_protected
- Reflectionproperty::is_private
The property name can be obtained by $property->getname ().
2) Get Comments:
A note to the property can be obtained by getdoccomment.
01foreach($properties as $property) {02if($property-isprotected ()) {03$docblock=$property-getdoccomment ();04Preg_match('/type\= ([a-z_]*)/',$property->getdoccomment (),$matches);05Echo $matches[1]. "\ n";06 }07 }08//Output:09//primary_autoincrement10//varchar11//text
3) methods to get classes
- GetMethods () to get all the methods to the class.
- Hasmethod (string) whether there is a method
- GetMethod (String) Get method
4) Method of executing the class:
1$instance->getname ();//execute the method in person GetName2//or:3$method=$class->getmethod (' GetName ');//get the GetName method in the person class4$method->invoke ($instance);//Execute the GetName method5//or:6$method=$class->getmethod (' SetName ');//get the SetName method in the person class7$method->invokeargs ($instance,Array(' snsgou.com '));
Second, through Reflectionmethod, we can get information about a method of the person class:
- "Public", "protected", "private", "static" type
- Parameter list of the method
- Number of parameters for the method
- Sung methods of using classes
1//Execute the Detail method2$method=NewReflectionmethod (' person ', ' test ');3 4if($method->ispublic () &&!$method-isStatic ()) {5Echo' Action is right ';6 }7Echo $method->getnumberofparameters ();//Number of parameters8Echo $method->getparameters ();//array of Parameter objects
PHP reflection Reflectionclass, Reflectionmethod Learning notes (i)