PHP Reflection Class Reflectionclass
What is a PHP reflection class, as the name implies, can be understood as a mapping of a class.
As an example:
Class Fuc {//define one of the classes
static function EC () {
Echo' I am a class ';
}
}
$class =new reflectionclass (' fuc '); Set up a reflection class for the Fuc class
Echo $class; Output this reflection class
$FUC = $class->newinstance (); //equivalent to instantiating the FUC class
$fuc->ec (); //implementation of FUC in the method EC
/* Final output: I am a class */
There are some more advanced uses.
$ec = $class->getmethod (' EC '); //Get the EC method in the Fuc class
$FUC = $class->newinstance (); //instantiation
$ec->invoke ($FUC); //Implementation of EC method
The above process is familiar. It's actually similar to the method that invokes the object
But here is the opposite, the method before, the object in the back
Example
[PHP]View Plaincopy
- try{
- If there is a class for the controller name
- if (class_exists ($this->getcontroller ())) {
- Construct a reflection class corresponding to a controller class using the Reflection API
- $RC = new Reflectionclass ($this->getcontroller ());
- If the class implements the IController interface
- if ($rc->implementsinterface (' IController ')) {
- The class has the method name pointed to by the parsed action string
- if ($rc->hasmethod ($this->getaction ())) {
- Constructs an instance of a controller class
- $controller = $RC->newinstance ();
- Gets the method object that the class $action parameter points to
- $method = $RC->getmethod ($this->getaction ());
- How the Reflection class Method object is called:
- $method->invoke ($controller);
- } Else {
- The following may throw an exception
- Throw New Exception ("Action");
- }
- } Else {
- Throw New Exception ("Interface");
- }
- } Else {
- Throw New Exception ("Controller");
- }
- }catch (Exception $e)
- {
- echo $e;
- }
PHP Reflection Class Reflectionclass