0

Possible Duplicate:
PHP Optional Parameters - specify parameter value by name?

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.

Community
  • 1
  • 1
Sergi Juanola
  • 6,531
  • 8
  • 56
  • 93
  • 3
    Why don't you pass an array as parameter ? – Ricardo Alvaro Lohmann Dec 12 '12 at 16:43
  • Using reflection would be the only option, but seems a pretty insane solution. You should structure your code so it is not necessary to pass arguments in a specific order that is not determinable by anything but introspection. – deceze Dec 12 '12 at 16:44
  • I'm trying to avoid that (it was my first approach). It's not actually for me, I'm doing a library and one of the problems is that the client wants it as easy as I can – Sergi Juanola Dec 12 '12 at 16:47
  • Have you run any metrics that reflection in PHP is not giving you the performance you need? It sounds a bit theoretic the problem you name here. – hakre Dec 12 '12 at 17:05
  • @hakre Omg, a total duplicate, and no, I haven't run any metrics, and it's completely theoretic, but I warned about that (quoting myself: _I'm concerned about execution times (which, **at least, in Java** tends to increase a lot when using Reflection)_ ) – Sergi Juanola Dec 12 '12 at 17:14
  • Well Java is Java and PHP is PHP. Also in PHP you will have some overhead but IIRC PHP reflection is very fast. *Edit:* For an example with reflection, see here [Can I pass an associative array as an argument to ReflectionMethod::invokeArgs?](http://stackoverflow.com/a/8649590/367456). – hakre Dec 12 '12 at 17:31

2 Answers2

1

To avoid this problem you should pass an array as parameter, like the following.
This way you'll not be stacked in a specific order and you can set defaults for the parameters that are not in the array.

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

exampleFunction($data);

function exampleFunction($params= array()) {
    $defaults = array(
        'param1' => '1',
        'param4' => '2',
        'param3' => '3',
        'param2' => '4',
    );

    $params += $defaults;
}

You can access an array element by the following.

$data['param1'], $data['param2'], $data['param3'], ...
deceze
  • 510,633
  • 85
  • 743
  • 889
Ricardo Alvaro Lohmann
  • 26,031
  • 7
  • 82
  • 82
0

If you're just trying to call the function using the values from the parameters, then I think this is what you want to do:

exampleFunction($params['param1'], $params['param2'], $params['param3'], $params['param4']);

Arrays in PHP act as maps too, so you can just use it like that.

shulmey
  • 101
  • 7