Application of PHP Reflection (Reflectionclass, Reflectionmethod) in the Controller scheduling module of thinkphp frame

Source: Internet
Author: User
Tags throw exception

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.