2

I'm currently looking for a way to give arguments to a function dynamically. Some code to explain:

//Class: ClassXY
PUBLIC function function1($arg1, $arg2, $arg3);
PUBLIC function function2($arg1);

I currently call it like this:

// $method = 'function1' or 'function2'
echo ClassXY :: {$method}($parameter_string);   

My problem is, that parameter_string either is a simple string, that contains the argument or an array that contains all the arguments I want to give to the function. For instance:

$parameter_string: array(3) {[0] => 'arg1', [1] => 'arg2', [2] => 'arg3' }

So far so good. My Problem is, that the call will only work with ClassXY :: function2() since it only expects one argument. If I call ClassXY :: function2() it wont work, since it expects three arguments but only one argument (the array with the three elements, that are supposed to be the three arguments) is given. I need a way to make php realize that each element of the array shall be one argument. Any Ideas?

Thanks in advance!

1
  • take a look at variadic functions (if you are using php 5.6): PHP 5.6 news Commented May 13, 2015 at 19:31

1 Answer 1

2

It looks like call_user_func_array is what you need.

http://php.net/manual/en/function.call-user-func-array.php

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.