What is a PHP reflection class, as its name suggests, can be understood as a mapping of a class.
As an example:
Class Fuc {//define a category
static function EC () {
Echo ' I am a class ';
}
}
$class =new reflectionclass (' fuc '); Create a reflection class of fuc this class
Echo $class; Output this reflection class
class [Class A] {@@ f:phpwebmyphptest.php 23-30-constants [0] {}-static properties [0] {}-static methods [0] { }-Properties [0] {}-Methods [1] {method [public method __construct] {@@ f:phpwebmyphptest.php 26-29}}}
$FUC = $class->newinstance (); Equivalent to the instantiation of the Fuc class
$fuc->ec (); Implementation of the FUC method EC
/* Final output: I am a class * *
And some of the more advanced uses
$ec = $class->getmethod (' EC '); Get the EC method in the Fuc class
$FUC = $class->newinstance (); Instantiation of
$ec->invoke ($FUC); Implementation of EC methods
The procedure above is very familiar. is actually similar to the method of calling the object
But here is the reverse, the method in front of the object in the back
Example
try{
If a class with a controller name exists
if (Class_exists ($this->getcontroller ()) {
Using the reflection API to construct a reflection class corresponding to a controller class
$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 resolved action string
if ($RC->hasmethod ($this->getaction ())) {
Constructing an instance of a controller class
$controller = $RC->newinstance ();
Gets the method object that the $action parameter of the class points to
$method = $RC->getmethod ($this->getaction ());
How the Reflection class method object is invoked:
$method->invoke ($controller);
} else {
The following are possible exceptions thrown
throw new Exception ("Action");
}
} else {
throw new Exception ("Interface");
}
} else {
throw new Exception ("Controller");
}
}catch (Exception $e)
{
Echo $e;
}