4
function myFunc($x, $y) {
    echo "x : {$x}";
    echo "y : {$y}";
}
$params = array("y" => 1, "x" => 2);

Is it possible to call myFunc like using call_user_func_array function but the keys of array will automatically set the right parameter in the function? Is there any function to do this or is it possible to create this function? for example :

call_func('myFunc', $params);

and the result would be like this

x : 2
y : 1

Thanks.

5
  • Have you actually tried anything? Commented Dec 6, 2012 at 15:08
  • 3
    I think you'd have to use reflections. Commented Dec 6, 2012 at 15:09
  • You can get parameters are myFunc($param); and then use extract($param); Commented Dec 6, 2012 at 15:11
  • @KevinM1 of course, but the result is x -> 1 and y -> 2 for the case above. Commented Dec 6, 2012 at 15:13
  • i don't want to use extract for this case. Commented Dec 6, 2012 at 15:21

2 Answers 2

7

There might be a simpler way to do this, but reflection should work too:

function call_func_keys($functionName, $args) {
    $f = new ReflectionFunction($functionName);
    $inOrder = array();

    foreach($f->getParameters() as $param) {
        if(array_key_exists($param->name, $args)) {
            $inOrder[] = $args[$param->name];
        } else {
            $inOrder[] = $param->getDefaultValue(); # Will throw a reflection exception if not optional
        }
    }

    call_user_func_array($functionName, $inOrder);
}

Here's a demo.

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

8 Comments

+1 for a good solution that does the job. Not sure I'd use it myself in production though; Reflection always leaves me feeling a bit dirty. What would the performance be like on this?
wow, that works perfectly. thanks! But, i'm getting a warning Warning: array_key_exists(): The first argument should be either a string or an integer in C:\xampp\htdocs\index.php on line 7. How to remove it?
@DarwinGautalius: Sorry, I had a previous version that accidentally used $param there; try it again.
@SDC Reflection is good because it doesn't actually instantiate the class or load the real function, of course it will be a small performance hit, but it'll be negligible, you won't be doing it in a loop with million iterations (i hope), otherwise you wouldn't be using PHP ;)
@DarwinGautalius: And, in case you're working with optional values, I just changed it so that it handles those better. If not, then ignore this :)
|
1

UPDATED!

Usually you should use call_user_func_array function.

And it should be used this way:

function myFunc($x, $y) {
   echo "x : {$x}";
   echo "y : {$y}";
}
$params = array("y" => 1, "x" => 2);

call_user_func_array('myFunc', $params);

But in your case, you definitely use Reflection. The @minitech answer is just great.

3 Comments

param_arr: The parameters to be passed to the callback, as an indexed array. So no, it doesn't work. codepad.viper-7.com/wQ7u8C
of course, but the result is x -> 1 and y -> 2 for the case above.
the whole point of the question is the he wants to do it in a way that call_user_func_array doesn't seem to support, and asking if there's a way to do it. Your answer doesn't give him anything beyond what he had already tried.

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.