This article mainly introduces the knowledge of PHP's reflection content. Here we provide relevant information explanations and simple sample codes for your reference. Interested friends can refer to
Recently, I have been reading about java programming ideas. See In the chapter on type information, we talk 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 comments." Of course the manual is a bit abstract! The so-called reverse engineering means that you can get detailed information about classes, methods, properties, 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 information related to a certain class, such as attributes, methods, namespaces, which interfaces are implemented, etc.! In the previous example, ReflectionClass::getProperties returned an array of ReflectionProperty objects.
ReflectionProperty Class reports information about the properties of the class. For example, isDefault isPrivate isProtected isPublic isStatic, etc., the method getName is to get the name of the attribute!
The above are 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 It is a ReflectionMethod type. The ReflectionMethod class reports information about a method, such as isAbstract isPrivate isProtected isPublic isStatic isConstructor, and there is also an important method Invoke, InvokeArgs is the execution method!
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!
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
phpDetailed explanation of WeChat public platform interaction and interface
## php Detailed explanation of file upload
##phpWeChat public account js-sdk development application
The above is the detailed content of Detailed explanation and example analysis of reflection in php. For more information, please follow other related articles on the PHP Chinese website!