Assign parameters to function according to parameter names

Posted by Korcholis on Stack Overflow See other posts from Stack Overflow or by Korcholis
Published on 2012-12-12T16:41:12Z Indexed on 2012/12/12 17:03 UTC
Read the original article Hit count: 122

Filed under:

I have an assoc array with a list of what are parameters to me. Here's an example:

array(
    'param1' => 'value1',
    'param4' => 'value4',
    'param3' => 'value3',
    'param2' => 'value2',
);

Note that they may come unsorted. Now, is there a way I can make a call (static or from an instance, using call_user_func_array or similar) and correctly pass each value to each parameter? Just to be sure, an example function I'd like to call using that parameter array is one such this:

exampleFunction($param1, $param2, $param3, $param4) {
    ...
}

PS: Reflection is great, but I'm concerned about execution times (which, at least, in Java tends to increase a lot when using Reflection). If you know any other way to do so, it would be awesome.

© Stack Overflow or respective owner

Related posts about php