Most of the answers supported the array method (which, generally speaking, I also agree), but I will play the devil's advocate and list some negatives:
The documentation is less clear.
Most methods for documenting functions / methods will list the parameters for this function individually. For example, a function with a basic DocBlock would look like this:
function accept_array($param_array = array('key1' => 'first_val', 'key2' => 'second_val')) { var_dump($param_array); }
Note that DocBlock does not support direct support for individual parts of $param_array , but the entire array as a whole. On the contrary, listing all arguments individually is as follows:
function accept_normal($key1 = 'first_val', $key2 = 'second_val') { echo $key1; echo $key2; }
This is also a problem if you expect your functions to be self-documenting enough, since in the first example you do not need to actually list the expected arguments in the function itself.
The default values may not work as expected.
“As expected” is probably a slightly loaded phrase (and this is probably one of the most obvious problems), but take the following:
function accept_array($param_array = array('key1' => 'first_val', 'key2' => 'second_val')) { var_dump($param_array); } accept_array(array('key2' => 'a_different_val'));
Some might expect var_dump to include the default value of key1 and the new value of key2 , but the whole array will be replaced, which means you will need to remember to set the default values for each key manually in each function, like this:
function accept_array($param_array = array()) { if (!isset($param_array['key1'])) { $param_array['key1'] = 'first_val'; } if (!isset($param_array['key2'])) { $param_array['key2'] = 'second_val'; } var_dump($param_array); } accept_array(array('key2' => 'a_different_val'));
No auto filtering
Enumeration of arguments the "normal" way also gives you a built-in set of filters. Take, for example, this quick and dirty user search:
function find_user($param_array = array('user_id' => 0, 'email' => '')) { foreach ($param_array as $field => $value) { $this->db->or_where($field, $value); } $this->db->get('users'); } find_user(array('first_name' => 'Joe', 'last_name' => 'Bloggs'));
Without adding a few "accepted key" type confirmations to $param_array calling the find_user() function can essentially use any fields that it likes. The simplest version will obviously look like this:
function find_user($user_id = 0, $email = '') { $this->db->or_where('user_id', $user_id); $this->db->or_where('email', $email); $this->db->get('users'); }
I agree that some of them are a bit of an entry level, and maybe I missed a lot more (feel free to comment more, and I will copy them in return with a loan), but I hope there is enough for people to think about automatic use twice "array method" without thinking about manual validation and documentation, etc.