1. How to get the module name and controller name through the address bar parameters (even if there is a route and the rewrite module is open)
2.TP is how to implement the pre-and post-method function modules, and how to execute the method with parameters?
PHP system comes with the Reflectionclass,reflectionmethod class, can reflect the user's custom class in the properties, methods, such as the permissions and parameters of information, through which can accurately control the execution of the method
Reflectionclass main methods of use:
Hasmethod (string) whether there is a method
GetMethod (String) Get method
Reflectionmethod Main methods:
Getnumberofparameters () Gets the number of arguments
Getparamters () get parameter information
3. Code Demo
Copy the Code code as follows:
Class indexaction{
Public Function index () {
Echo ' index '. \ r \ n ";
}
Public function test ($year =2012, $month =2, $day =21) {
echo $year. '--------'. $month. '-----------'. $day. " \ r \ n ";
}
Public Function _before_index () {
echo __function__. " \ r \ n ";
}
Public Function _after_index () {
echo __function__. " \ r \ n ";
}
}
Execute the Index method
$method = new Reflectionmethod (' indexaction ', ' Index ');
Make permission Judgments
if ($method->ispublic ()) {
$class = new Reflectionclass (' indexaction ');
To perform a predecessor method
if ($class->hasmethod (' _before_index ')) {
$beforeMethod = $class->getmethod (' _before_index ');
if ($beforeMethod->ispublic ()) {
$beforeMethod->invoke (new indexaction);
}
}
$method->invoke (new indexaction);
Performing a post-placement method
if ($class->hasmethod (' _after_index ')) {
$beforeMethod = $class->getmethod (' _after_index ');
if ($beforeMethod->ispublic ()) {
$beforeMethod->invoke (new indexaction);
}
}
}
Execute a method with parameters
$method = new Reflectionmethod (' indexaction ', ' 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 (New indexaction, $args);
Else
Echo ' parameters is not match! ';
http://www.bkjia.com/PHPjc/825264.html www.bkjia.com true http://www.bkjia.com/PHPjc/825264.html techarticle 1. How to get the module name and controller name through the address bar parameters (even in the case of Routing and Rewriting module) 2.TP is how to implement the pre-and post-method function module, and such as ...