如何在php中使用数组中的变量参数调用实例变量方法

问题描述:

I want to call a function with variable length arguments stored in an array. I noticed that you can do this with call_user_func_array($callback, $array); However it doesn't seem to work on instance variable methods.

class foo{
  $iVar 

  function A{
      $anArray = array(...);
      call_user_func_array(iVar->methodName,$anArray);
  }
}

Any suggestions?

A couple of things are wrong there. First, iVar is not a constant, so it should begin with a $. And since it is a property of foo, it should be $this->iVar.

Secondly, you cannot pass a function like that. You have to pass it as a callable. So in total, the call should look like this:

call_user_func_array(array($this->iVar, 'methodName'), $anArray);