http://php.net/manual/en/function.call-user-func.phpcall_user_func (PHP 4, PHP 5)call_user_func — Call a user function given by the first parametermixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] ) Call a user defined function given by the function parameter.
function d(){ static $api; if(!$api) { $api = new api(); } $args = func_get_args(); $fun = array_shift($args); if(method_exists( $api,$fun)) { call_user_func_array(array($api,$fun),$args); } else { echo "You are calling a unknow method '{$fun}'"; } }class api { function a($x1,$x2) { $args = func_get_args(); echo implode(' ', $args); }
func_get_arg
func_get_args
func_num_args
(PHP 4, PHP 5)call_user_func — Call a user function given by the first parametermixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )
Call a user defined function given by the function parameter.
static $api;
if(!$api)
{
$api = new api();
}
$args = func_get_args();
$fun = array_shift($args);
if(method_exists( $api,$fun))
{
call_user_func_array(array($api,$fun),$args);
}
else
{
echo "You are calling a unknow method '{$fun}'";
}
}class api
{
function a($x1,$x2)
{
$args = func_get_args();
echo implode(' ', $args);
}
function b($x1,$x2,$x3,$x4,$x5)
{
$args = func_get_args();
echo implode(' ', $args);
}
}d('a','hello','world');
echo "<br/>";
d('b','hey','dude','are','you','there?');
echo "<br/>";
d('c','???');
function d() {
$p = func_get_args();
$fun = array_unshift($p);
if( method_exists($this, $fun))
return call_user_func_array(array($this, $fun), $p);
if(function_exists($fun))
return call_user_func_array($fun, $p);
exit("方法(函数) $fun 未定义");
}
}