[英]Repassing unknown number of arguments
I have this little function that I use to call some functions in my class. 我有这个小函数,可以用来在课堂上调用某些函数。
public static function call($method){
$Model = new Model();
if(method_exists($Model, $method)){
return $Model->{$method}();
}
}
Now, my question is about the args passed. 现在,我的问题是关于传递的参数。 I want to repass them, and I don't want an array to be passed, but the actual arguments. 我想重新传递它们,并且我不希望传递数组,而是传递实际参数。
I know the function func_get_arg() and func_num_args(), but this doesn't work: 我知道函数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);
Is there any alternative method i could call and pass in $Model->{$method}(passed_args)? 我可以调用$ Model-> {$ method}(passed_args)并传递其他方法吗?
UPDATE UPDATE
I tried altering the method to this, but it doesn't work: 我尝试更改此方法,但不起作用:
public static function call($method){
$Model = new Model();
$args = func_get_args();
if(method_exists($Model, $method)){
return call_user_func_array(array($Model, $method), $args);
}
}
It works if I do this, because until now I only have one arg or none: 如果这样做,它会起作用,因为直到现在我只有一个arg或一个都没有:
public static function call($method, $args = null){
$Model = new Model();
if(method_exists($Model, $method)){
return $Model->{$method}($args);
}
}
Solution: 解:
Of course I have to alter the method call: 当然,我必须更改方法调用:
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);
}
}
The above works. 以上作品。 Thank you. 谢谢。
function foo() {
$args = func_get_args();
return call_user_func_array(array($model, $method), $args);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.