thinkphp controller scheduling using the sample _php tutorial

Source: Internet
Author: User
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 ...

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