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
EDIT: I would also address you some question/issues concerning array parameter. In particular if you have the same answer than @Brent Baisley and @ircmaxell
- What if you misspell a key in the array?
- What if you have some key that don't have any meaning to your function/method?