This article mainly introduces the knowledge of the content of the reflection of PHP, here to provide relevant information to explain, and simple sample code for your reference, interested in the small partners can refer to the next
Recently, looking at the Java programming idea, see the type Information chapter, the information of the class and the concept of reflection. By the way, let's take a look at PHP's reflective stuff. The manual says: "PHP 5 has a complete reflection API that adds the ability to reverse engineer classes, interfaces, functions, methods, and extensions." In addition, the reflection API provides a way to remove document comments from functions, classes, and methods. "Of course the handbook says something abstract!" The so-called reverse white is the ability to get detailed information about classes, methods, properties, parameters, and so on, including comments! Words are always so dull, 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 about a class, such as attributes, methods, namespaces, implementations of those interfaces, and so on! In the previous example Reflectionclass:: GetProperties Returns an array of Reflectionproperty objects.
The Reflectionproperty class reports information about the properties of the class. such as IsDefault isprivate isprotected isPublic isstatic, Method GetName is to get the name of the property!
The above is the acquisition of properties, as well as the acquisition of 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, there is an important way Invoke,invokeargs is the execution Method!
Other objects can look at manuals, not hard!
What is the purpose of reflection?
Reflection is a concept of dynamic operation that can be used to help us analyze other classes, interfaces, methods, properties, methods, and extensions. You can also build patterns, such as dynamic proxies. The use of reflection in some PHP frameworks is also very common, such as Kohana,yii, the following is the implementation of MVC code Kohana, is the use of 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$met Hod = $class->getmethod (Router:: $method);//Execute the Controller Method$method->invokeargs ($controller, $ arguments);
The above code can clearly see the flow of this framework! Through the router actually processing the URL class, through the router can get which controller, which method! And then execute the method!
Summary: The above is the entire content of this article, I hope to be able to help you learn.