Php 重新处理未知数量的参数
我有一个小函数,用来调用类中的一些函数Php 重新处理未知数量的参数,php,arguments,Php,Arguments,我有一个小函数,用来调用类中的一些函数 public static function call($method){ $Model = new Model(); if(method_exists($Model, $method)){ return $Model->{$method}(); } } 现在,我的问题是关于通过的ARG。我想重新通过它们,我不想传递数组,而是要传递实际的参数 我知道函数func_get_arg()和func_num_args
public static function call($method){
$Model = new Model();
if(method_exists($Model, $method)){
return $Model->{$method}();
}
}
现在,我的问题是关于通过的ARG。我想重新通过它们,我不想传递数组,而是要传递实际的参数
我知道函数func_get_arg()和func_num_args(),但这不起作用:
$args = '';
for($i=0; $i<func_num_args(); $i++){
$args .= func_get_args($i).',';
}
$args = substr($args, 0, strlen($args)-1);
如果我这样做,它会起作用,因为直到现在我只有一个arg或没有:
public static function call($method, $args = null){
$Model = new Model();
if(method_exists($Model, $method)){
return $Model->{$method}($args);
}
}
解决方案:
当然,我必须更改方法调用:
public static function call(){
$Model = new Model();
$args = func_get_args();
$method = array_shift($args);
if(method_exists($Model, $method)){
return call_user_func_array(array($Model, $method), $args);
}
}
以上工作。谢谢。谢谢,现在可以用了:)
function foo() {
$args = func_get_args();
return call_user_func_array(array($model, $method), $args);
}
function foo() {
$args = func_get_args();
return call_user_func_array(array($model, $method), $args);
}