Home > Article > Backend Development > Detailed explanation and sample code of reflection in php
I was reading Java Programming Thoughts recently and saw the chapter on type information, which talked about class information and the concept of reflection. By the way, let’s review the reflection tools of php. Here's what the manual says: "PHP 5 has a complete reflection API, adding the ability to reverse engineer classes, interfaces, functions, methods, and extensions. In addition, the reflection API provides methods to take out reflections in functions, classes, and methods." Documentation comment. "Of course the manual is a bit abstract! The so-called reverse engineering is to get detailed information about classes, methods, attributes, parameters, etc., including comments! The text is always so boring, for example
class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; /** * Enter description here ... */ public function myMethod() { echo 'hello 2b'; } } $ref = new ReflectionClass('Foo'); $props = $ref->getProperties(); foreach ($props as $value) { echo $value->getName()."\n"; } //output //foo //bar //baz
ReflectionClass This class returns relevant information about a certain class, such as attributes , methods, namespaces, implementing those interfaces, etc.! In the previous example, ReflectionClass::getProperties returned an array of ReflectionProperty objects. The
ReflectionProperty class reports information about the properties of a class. For example, isDefault isPrivate isProtected isPublic isStatic, etc., the method getName is to get the name of the attribute!
The above is for obtaining attributes, and there are also methods for obtaining class methods, such as
class Foo { public $foo = 1; protected $bar = 2; private $baz = 3; /** * Enter description here ... */ public function myMethod() { echo 'hello 2b'; } } $ref = new ReflectionClass('Foo'); $method = $ref->getMethod('myMethod'); $method->invoke($ref->newInstance());
ReflectionClass::getMethod is a ReflectionMethod type. The ReflectionMethod class reports information about a method, such as isAbstract isPrivate isProtected isPublic isStatic isConstructor, and there is The important methods Invoke and InvokeArgs are the execution methods!
For other objects, you can read the manual, it is not difficult!
What are the uses of reflection?
Reflection is a dynamically running concept. Used together, they can be used to help us analyze other classes, interfaces, methods, properties, methods and extensions. Patterns can also be built, such as dynamic proxies. It is also very common to use reflection in some PHP frameworks, such as kohana and yii. The following is the code of kohana to implement mvc, which uses reflection!
// Start validation of the controller $class = new ReflectionClass(ucfirst(Router::$controller).'_Controller'); // Create a new controller instance $controller = $class->newInstance(); // Load the controller method $method = $class->getMethod(Router::$method); // Execute the controller method $method->invokeArgs($controller, $arguments);
The above code can clearly see the process of this framework! Through Router, you actually process the url class. Through Router, you can get which controller and which method! Then execute the method again!
The above is the collection of information on PHP reflection. We will continue to add relevant information in the future. Thank you for your support of this site!
For more PHP reflection details and sample code related articles, please pay attention to the PHP Chinese website!