This article mainly introduces the PHP Reflection mechanism, and analyzes in detail the functions, principles, attributes, comments, and methods of the php Reflection mechanism in combination with examples, for more information about the PHP Reflection mechanism, see the next article. I analyzed the functions, principles, attributes, comments, and methods of the php Reflection mechanism in detail using examples, for more information, see
This article describes the PHP Reflection mechanism. We will share this with you for your reference. The details are as follows:
Introduction:
PHP5 adds a new feature: 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
Example
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; }}
Next, you only need to pass the class name "Person" to ReflectionClass:
$ Class = new ReflectionClass ('person '); // creates a reflection class for the Person class $ instance = $ class-> newInstanceArgs ($ args); // It is equivalent to instantiating the Person class
1) 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.
Pass$property->getName()You can get the property name.
2) get comments:
You can use getDocComment to get comments to the property.
foreach($properties as $property) { if($property->isProtected()) { $docblock = $property->getDocComment(); preg_match('/ type\=([a-z_]*) /', $property->getDocComment(), $matches); echo $matches[1]."\n"; }}// Output:// primary_autoincrement// varchar// text
3) obtain the class method
Method:getMethods()To obtain all the methods of the class.
4) execution methods:
$ Instance-> getBiography (); // execute the getBiography method in Person // Or: $ ec = $ class-> getmethod ('getname '); // Get the getName method $ ec-> invoke ($ instance) in the Person class; // execute the getName method
The above is the sample code analysis of PHP Reflection mechanism. For more information, see other related articles in the first PHP community!