How does the controller module of the thinkphp framework implement the Front controller , the rear controller , and how to implement the method with parameters?
PHP system comes with the Reflectionclass, Reflectionmethod class, can reflect the user custom class in the attributes, methods, such as the permissions and parameters of information, through which can accurately control the implementation of the method.
reflectionclass: [PHP manual] Details
The main method of use:
Hasmethod (string) whether there is a method
GetMethod (String) Get method
reflectionmethod: [PHP manual] Details
Main methods:
IsPublic () is public method
Getnumberofparameters () Gets the number of arguments
Getparamters () get parameter information
Invoke (object $object [, mixed $parameter [, mixed $ ...]) execution method
Invokeargs (object obj, array args) with parameter execution method
Example Demo:
<?phpclass blogaction {public Function detail () {echo ' detail '. "\ r \ n";} Public function test ($year =, $month = 4, $day = +) {echo $year. ‘--‘ . $month. ‘--‘ . $day. "\ r \ n";} Public Function _before_detail () {echo __function__. "\ r \ n";} Public Function _after_detail () {echo __function__. "\ r \ n";}} Execute Detail Method $method = new Reflectionmethod (' blogaction ', ' detail '); $instance = new Blogaction ();//permission to determine if ($method-& Gt;ispublic ()) {$class = new Reflectionclass (' blogaction ');//Perform the predecessor method if ($class->hasmethod (' _before_detail ')) {$ Beforemethod = $class->getmethod (' _before_detail '), if ($beforeMethod->ispublic ()) {$beforeMethod->invoke ( $instance);}} $method->invoke (new blogaction),//Execute the Post method if ($class->hasmethod (' _after_detail ')) {$beforeMethod = $class GetMethod (' _after_detail '), if ($beforeMethod->ispublic ()) {$beforeMethod->invoke ($instance);}} Execute the method with parameters $method = new Reflectionmethod (' blogaction ', ' Test '), $params = $method->getparameters (); foreach ($paRams as $param) {$paramName = $param->getname (), if (Isset ($_request[$paramName)) {$args [] = $_request[$paramName];} ElseIf ($param->isdefaultvalueavailable ()) {$args [] = $param->getdefaultvalue ();}} if (count ($args) = = $method->getnumberofparameters ()) {$method->invokeargs ($instance, $args);} else {echo ' Parameters is wrong! ';}
" Another reference code "
/** * Execute App Controller */public function Execapp () {//Create action Controller Instance $classname = module_name. ' Controller '; $namespaceClassName = ' \\apps\\ '. App_name. ' \\controller\\ '. $className; Load_class ($namespaceClassName, False), if (!class_exists ($namespaceClassName)) {throw new \exception (' oops! Module not found: '. $namespaceClassName);} $controller = new $namespaceClassName ();//Gets the current operation name $action = action_name;//performs the current Operation//call_user_func (Array (&$ Controller, $action)); In fact, with this function is enough!!! try {$methodInfo = new \reflectionmethod ($namespaceClassName, $action); if ($methodInfo->ispublic () &&!$ Methodinfo->isstatic ()) {$methodInfo->invoke ($controller);} else {//action method not public type, throw exception throw new \ Reflectionexception ();}} After catch (\reflectionexception $e) {//Method call exception, boot to __call method processing $methodinfo = new \reflectionmethod ($namespaceClassName, ' _ _call '); $methodInfo->invokeargs ($controller, Array ($action, '));} return;}
Application of PHP reflection (Reflectionclass, Reflectionmethod) in the Controller scheduling module of the thinkphp framework