tags:

views:

153

answers:

5

CakePHP makes heavy use of associative arrays for passing large numbers of parameters to functions. I have not really seen this technique outside of PHP and never seen it used to the extent that Cake uses it. I really like this approach because as it seems it would be easier to handle new parameters in future releases of your own code and it's alot more readable than simply a long list of params.

As an example...

function myFunc($params = array('name' => 'rob', 'count' => 5, 'anArray' => array('A string', 5, myObject)))
{
    // ...
}

I guess this is similar to using argc/argv, but is a bit easier to read. Does anyone have a list of pros and cons on this method or know of anyone that's written on best practices with this? I've tried just Googling it but "associative array of parameters" brings up pretty much every programming article ever written.

Also, is there even a term for passing parameters this way?

+2  A: 

It is a emulation of using keyword arguments. In python, for example, you can say:

myFunc(name="bob", age=10, gender="male")

Since PHP does not support this syntax, associative arrays are the next-best-thing.

gahooa
Thanks a ton, just having a name for it really helped. For some reason I never made the connection with named arguments (aka keyword arguments) which i run into all the time with Obj C
rob5408
+2  A: 

Other high-level languages support named parameters to functions and methods. For instance, in Python you can call:

my_func(name='rob', count=5, an_array=['A string', 5, my_object])

What you are seeing is an attempt to simulate this behavior in PHP. The benefits are obvious.

  • Flexibility
  • No need to know order/number of expected parameters

One drawbacks might be that a hash table lookup is required for every method call, but depending on how arguments are handled in PHP, the performance hit may be negligible.

sixthgear
A: 

It's the only way to do named parameters in PHP.

It's only useful when you have a large number of arguments, IMO. Otherwise the benefits of an explicitly defined argument signature are the better choice, especially if you use an "intellisense" capable IDE.

Peter Bailey
+1  A: 

A downside to using named parameters is documenting the parameters with PHPDoc. Many editors/IDEs provide "automatic" documentation that can parse your code and generate generic docblocks.

e.g.

function foo(array $bar, SomeClass $stuff) { returns $magic; }

would produce:

/**
 * foo
 *
 * @param array $bar
 * @param SomeClass $stuff
 * @return mixed
 */
function foo(array $bar, SomeClass $stuff) { returns $magic; }

If you put all your parameters in a $params array, it would only look like

/**
 * foo
 *
 * @param array $params
 * @return mixed
 */

It also adds a lot of additional burden to your developers to have to type the extra code for each parameter. I'd suggest using a mix of the two approaches.

e.g. If you had a function to return an HTML text input element you could have the following method signature:

/**
 * formText
 *
 * @param string $name name of text element
 * @param string $value value of text element
 * @param array $options additional options for text element
 * @return string
 */
function formText($name, $value, $options = array());

Thus, you can easily pass the most common values to the function.

$this->formText('foo', 'Default...');

and if you need additional, less common params you'd use the convenient named parameter syntax:

$this->formText('foo', 'Default...', array(
    'class' => 'bold highlighted'
));

otherwise, using only a generic $params array you'd have to type:

$this->formText(array(
    'name' => 'foo',
    'value' => 'Default...'
));
hobodave
A: 

This technique is (really) often used in Javascript, with objects (when you are using string as keys, in JS, you are using objects, not arrays) ; for an example, see scriptaculous Draggable.

Now, for a couple of pros and cons that immediatly come to mind :

  • pro : you can use any number of parameters you want, and still have them named
    • and there is no "only parameters at the end of the list are optionnal"
  • con : phpdoc is not fine : it seems there is only one parameter, and there is not much indication on what it does
  • con (consequence of the previous one) : when you are using an IDE with code-hints, it cannot display names/description for each parameter : you always have to check the documentation.

That single "con" is enough for me : I only use that way of passing parameters if there is no other (reallistic) way.

Pascal MARTIN