Application of PHP Reflection ReflectionClass and ReflectionMethod in the controller scheduling module of ThinkPHP framework

Source: Internet
Author: User
: This article mainly introduces the application of PHP Reflection ReflectionClass and ReflectionMethod in the controller scheduling module of the ThinkPHP framework. if you are interested in the PHP Tutorial, refer to it. How does the controller module of the ThinkPHP framework implement the pre-controller and post-controller, and how does it implement the method with parameters?

The ReflectionClass and reflemethod method classes provided by the PHP system can reflect attributes of user-defined classes, permission and parameters of methods, and accurately control the execution of methods.

ReflectionClass: [PHP Manual] details

Mainly used methods:

HasMethod (string) whether a method exists

GetMethod (string)

ReflectionMethod: [PHP Manual] details

Main methods:

Whether isPublic () is a public method

GetNumberOfParameters ()

GetParamters () get parameter information

Invoke (object $ object [, mixed $ parameter [, mixed $...]) execution method

InvokeArgs (object obj, array args) with parameter execution method

Example:

 IsPublic () {$ class = new ReflectionClass ('blogaction'); // execute the pre-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 () {$ beforeMet Hod-> 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]

 IsPublic () {$ class = new ReflectionClass ('blogaction'); // execute the pre-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 () {$ beforeMet Hod-> 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! ';}

The above introduces the application of PHP Reflection ReflectionClass and ReflectionMethod in the controller scheduling module of the ThinkPHP framework, including some content, and hopes to help friends who are interested in PHP tutorials.

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.