: 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.