PHP function: passing parameters

Firstly, I want to say that I'm not trying to start a war here. I know that from my own conversations on the topic that follows me, there may be some convinced opinions, so I am looking for an answer that outlines the pros and cons.

In freely typed languages ​​(and I specifically come from the point of view of PHP) there are two ways to pass parameters in design parameters (note, I'm not talking about getting parameters in called functions).

On the one hand, you can pass them as individual parameters, as shown below:

foo($var1, $var2, $var3 ...);

On the other hand, you can pack your variables into an array, and then pass one argument to the function, an array:

 $bar[] = $var1;
 $bar[] = $var2;
 $bar[] = $var3;
 ...

 foo($bar);

, , , , .

, , , , , .

, , , , , , ? , , , , .

+3
1

, . , .

, , :

function x($width = 100, $height = 100, $color = 'red')

, , , :

x(100, 100, 'blue');

.. .

:

function x(array $opts) {
    $opts += array('width' => 100, 'height' => 100, 'color' => 'red');
}
x(array('color' => 'blue'));

Twig_Environment: , , , , . , , .

+2

All Articles