views:

179

answers:

5

is it considered 'bad practice' to create an fucntion like so:

// $arr_member_fields['first_name'] = $_POST['first_name'];
// $arr_member_fields['last_name']  = $_POST['first_name'];
// $arr_member_fields['email']      = $_POST['email'];
// $arr_member_fields['dob']        = $_POST['dob'];
// $arr_member_fields['gender']     = $_POST['gender'];

function update_member($int_member_id $arr_member_fields)
{
    //some code
}

or should a create a function with no array and just use variables instead -- like so:

function update_member($int_member_id, $str_first_name, $str_last_name, str_email, $str_dob, $chr_gender)
{
    //some code
}

the reason why i prefer the first method (the one with the array) is that i always have the option to loop through the array for database insertion/updating purposes.

very curious to know other peoples inputs on this.

+1  A: 

Depends on the function. There is no best practice here that will adequately capture most cases when writing a function. What if you don't need to loop through the arguments?

Seems like you're best off passing arrays in this case.

jacobangel
A: 

This is a case where I would consider a structure to hold the data. That way I can pass the structure or an array of structure to a methods or I can access individual elements of the structure.

Of course if I wanted to go further I'd made a class instead of a structure. Then I can have methods as well as data, setters, getters with validation, and so on.

Jim C
A: 

Things like this ultimately come down to effects on performance and code readability. While passing an array is extremely handy, it also makes it difficult to follow for someone else reading your code (or even for you if you're coming back to it 6 months later). This is especially true if the array is defined somewhere else in a large file, or another file altogether.

At the end of the day it really comes down to why you're trying to optimize your code in the first place. Is it for performance, or to make it easier to understand/read?

If it's for readability, pick the approach that seems easiest to understand. You'll thank yourself when you have to go back and tweak something months from now.

If it's for performance, make sure you actually need to improve performance and that you aren't optimizing something just for the sake of optimization. More often than not, you'll create a very real readability problem in an effort to solve a nonexistent performance problem.

Wilco
A: 

I would go with the second method because your IDE can tell you what parameters it should take without having to go to that file. It might seem ok to use an array now for a small project, but as your project becomes larger its likely to become a problem later. It will also save you from asking yourself later when you're trying to use it "Did I use name_ first, or first_ name?" Those fields look like something that would all be in one table, which would be updated in a single statement anyway, so I don't see any reason you would want to loop over the array.

Asa Ayers
A: 

I think that passing parameters by array is generally a bad idea. If parameter lists are so long that they justify being passed as an array it's probably a sign that the code is not designed in the best way.

Passing parameters in an array is more difficult to document using something like PHPDocumentor and will also not be picked up by the code-completion in an IDE. Passing parameters explicity also allows for type hinting and is more transparent to the reader of the code.

Obviously if you are passing an array of items to be processed as a function argument then it makes sense to pass it as an array.

Tim Wardle