I am currently working on a project on the Codeigniter MVC framework. I have created an authentication library to suite the project with several functions. I will be checking and validating data in my controller and then passing the data into one of these functions. For example my register function takes about 5 parameters at the moment, but should I pass these in as strings or should I pass them in as an array? Is there a set rule or is this personal preference?
2 Answers
It's personal preference. If you are passing the parameters to a function, it can be easier to pass them in an array if there are quite a few parameters. Also, passing the parameters in an array means you don't have to pass them to a function in a specific order as you must do when passing each parameter to a function individually.
Example...
function myFunc($param1, $param2, $param3, $param4, $param5) { ... }
For the above, you must pass all params in the exact order...
myFunc('Something', '', 'Something Else', '', '');
An easier way is to pass the parameters in an array...
function myFunc($array) { ... }
The $array can contain all or some of the parameters in any order...
$array { 'param1' => 'Something', 'param3' => 'Something Else' ... }
You just have to make sure your function addresses missing parameters gracefully if you are passing them via an array...
if (empty($array['param1'])) return false; // or whatever should happen