PHP callback function parameter (solve the problem of the Eval function stitching object parameters)

Source: Internet
Author: User

When using Smarty, you define a function that calls the controller uniformly, as follows:

function The name of the C ($name$method) {//Controller and the name     of the method require_once "controller/".  $name. " Controller.class.php ";//Introduce    the controller eval(' $obj = new ').  $name. ' Controller (); $obj. $method. ' ();‘ );//Instantiate the controller and invoke the method}

But this function can only call the function without parameters in the controller, but I want to use this function to pass in the parameter, and then pass the parameter into the controller, and transform the following

function C ($name$method$arr = []) {    require_once "controller/".  $name. " Controller.class.php ";     $params implode $arr );     Eval (' $obj = new '. $name. ' Controller (); $obj. $method. ' (‘. $params. '); ' );}

Because you have to consider more than one parameter, all of the third parameter is set to an array type, which will automatically be split into the "param1, param2, param3" format.

now that's the problem. If an object is passed in, the system cannot turn the object into a string, and if forced incoming will give an error, all

Eval (' $obj = new '. $name. ' Controller (), $obj-$method. ' ('. $params. '); ');

In a way that doesn't have any eggs.

In order to solve the problem of the incoming parameter as an object, recall the callback function
functionC$name,$method,$arr= []){    require_once"controller/".$name." Controller.class.php "; $params= ""; if(!Is_object($arr)){        $params=implode(",",$arr); Eval(' $obj = new '.$name.‘ Controller (); $obj.$method.‘ (‘.$params.‘);‘); }Else{        Eval(' $obj = new '.$name.‘ Controller (); '); Call_user_func(Array($obj, "Showsmarty"),$arr); }}

It is not an object to be judged by the parameters passed in first, if it is called by the callback function, otherwise, it is called in the way of the previous array.

Problem solved!!!

PHP callback function parameter (solve the problem of the Eval function stitching object parameters)

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.