Passing an array for setting variable

Posted by mathk on Stack Overflow See other posts from Stack Overflow or by mathk
Published on 2010-06-07T15:13:06Z Indexed on 2010/06/07 16:02 UTC
Read the original article Hit count: 268

Hi,

I often see this idiom when reading php code:

public function __construct($config)
{
    if (array_key_exists('options', $config)) {
       ...
    }
    if (array_key_exists('driver_options', $config)) {
        ...
    }
}

Here I am concern with the way the parameter is used. If I were in lisp I would do:

(defun ct (&key options driver_options) 
       (do-something-with-option-and-driver_option))

But since I am in PHP I would rather have a constructor that take a list of parameter and let them be null if there a not require.

So what do you guys think about having an array as parameter in other to do some initialization-or-whatever?

In other to answer you have to take in account the point of view of the user of the function and the designer of the API.

Also have you ever heard this has a code-smell?

thanks

© Stack Overflow or respective owner

Related posts about php

Related posts about design