1

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!

9

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.