FlyingCat
FlyingCat

Reputation: 14290

How to pass array of arguments to an method

I am trying to pass array to a method and I want the method use array's values as arguments.

I posted a similar question on How to assign arrays to functions arguments? thread but I am actually working on the class method here.

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.
}

Any thoughts? Thank a lot!

Upvotes: 1

Views: 176

Answers (1)

prodigitalson
prodigitalson

Reputation: 60413

As Felix suggested you need to pass a callable. For a class method that is an array with the object inatance (or class name if it is a static method) and the method name:

call_user_func_array(array($obj,'myMethod'), $args)

Upvotes: 3

Related Questions