0

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?

1
  • There's no set rule. Most programs use separate arguments. An array is useful if a function takes lots of parameters, you can use an associative array to name them instead of having to remember the order. Commented Apr 30, 2015 at 17:53

2 Answers 2

1

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 
Sign up to request clarification or add additional context in comments.

Comments

0

Well it's up to you, but with an array you have to check yourself that proper indices were set to proper types. With individual parameters (and possibly type hints) php will do this for you. Also individual parameters work better with IDE.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.