Such as:
I have three ways to do it now.
foo($a,$b=null)boo($a=array(),$b="")Roo($a)
Now I just need to pass the method name and the argument list to call the method directly.
Hopefully this is possible (pseudo code):
function run_function($fun_name,$paras){ {$fun_name}(....//这里应该怎么写呢?);}
I found the correct answer, thanks to the following two replies, but really not the answer I need. The solution is now posted:
$handler = array( 'test', 'test3');$params = array( array(1,2,3,4), 1);if($params == null){ call_user_func($handler);}if (is_callable($handler) ){ call_user_func_array( $handler , $params );}class test{ function test1($arr,$arr1) { var_dump(array_merge($arr,$arr1)); } function test2() { echo 123; } function test3($arr,$a) { var_dump($arr); var_dump($a); }}
Reply content:
Such as:
I have three ways to do it now.
foo($a,$b=null)boo($a=array(),$b="")Roo($a)
Now I just need to pass the method name and the argument list to call the method directly.
Hopefully this is possible (pseudo code):
function run_function($fun_name,$paras){ {$fun_name}(....//这里应该怎么写呢?);}
I found the correct answer, thanks to the following two replies, but really not the answer I need. The solution is now posted:
$handler = array( 'test', 'test3');$params = array( array(1,2,3,4), 1);if($params == null){ call_user_func($handler);}if (is_callable($handler) ){ call_user_func_array( $handler , $params );}class test{ function test1($arr,$arr1) { var_dump(array_merge($arr,$arr1)); } function test2() { echo 123; } function test3($arr,$a) { var_dump($arr); var_dump($a); }}
http://www.php.net/manual/zh/function.func-get-args.php
function run_function($fun_name){ if(function_exists($fun_name)) { $num = func_num_args(); $args = func_get_args(); array_shift($args); return $num==1 ? call_user_func($fun_name) : call_user_func_array($fun_name,$args); } trigger_error('Call to undefined function ' . $fun_name, E_USER_ERROR);}//使用run_function('foo', $a, $b);run_function('boo', $a, $b);run_function('Roo', $a);
The ignored answer can be fulfilled.