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)