Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将参数数组传递给方法_Php_Oop - Fatal编程技术网

Php 如何将参数数组传递给方法

Php 如何将参数数组传递给方法,php,oop,Php,Oop,我试图将数组传递给一个方法,我希望该方法使用数组的值作为参数 我在网上发布了一个类似的问题 线程,但实际上我正在处理这里的类方法 if (method_exists($testInstance, $method)){ $a=array {'1'=>'aru1', '2'=>'arg2'} //could have more than 2. it's dynamic call_user_func_array($testInstance->$method(), $a);

我试图将数组传递给一个方法,我希望该方法使用数组的值作为参数

我在网上发布了一个类似的问题 线程,但实际上我正在处理这里的类方法

if (method_exists($testInstance, $method)){
  $a=array {'1'=>'aru1', '2'=>'arg2'}   //could have more than 2. it's dynamic
  call_user_func_array($testInstance->$method(), $a);  //this won't work.
}

有什么想法吗?非常感谢

正如Felix所建议的,您需要通过一个callable。对于一个类方法,该类方法是一个
数组
,其对象为inatance(如果是静态方法,则为类名)和方法名:


调用\u user\u func\u数组(数组($obj,'myMethod'),$args)

Mmmh的可能重复项。。。您是否考虑过阅读文档(第二个示例)。。。阅读更多链接也很有帮助,阅读关于
callbable
:可能重复的--second答案。@FelixKling它似乎只在静态方法中有效。我错了吗?用正常的方法很好。该示例显示:
/*使用两个参数调用$foo->bar()方法*/$foo=newfoo;调用用户函数数组(数组($foo,“bar”)、数组(“三”、“四”)。这也是以前问过的问题,答案是:。为什么你认为这个方法必须是静态的?