Jump to content

Passing Parameters as Variables vs Passing Parameters as An Array


NotionCommotion

Recommended Posts

I have some function or method.  Is there a better design patter to implement this?

function myFunction($a=null,$b=null,$c=null,$d=null,$e=null,$f=null,$g=null,$h=null)
{
   //Do a bunch of stuff
}

Maybe the second function?

function myNewFunction($data=array())
{
   $data=array_merge(array('a'=>null,'b'=>null,'c'=>null,'d'=>null,'e'=>null,'f'=>null,'g'=>null,'h'=>null),$array);
   //Do a bunch of stuff
}

Please provide decision making factors why you would use one approach over the other.

It's a preference, I guess. Most people I know prefer using objects rather than a large quantity of parameters. Although when opting for using an Array as the parameter, you should probably change it out to

function myNewFunction(Array $data = array()) {

So that any other type of value is not accepted by the function. Right now you could call myNewFunction('Test'); and it will attempt to merge an array with a string.

 

Since you asked for personal preferences as well, I'll mention that I'd always opt for the object parameter option, simply because it's easier, far more readable, and like you just demonstrated yourself it's the perfect parameter when used for things like Configurations/Options (Database, for instance).

 

It's especially nice when using getters/setters in a class as well. For instance:

class MyClass {
private $_defaults = array(); //Default values
 
public function __construct() {
 $this->setData($this->_defaults);
}
 
public function myNewFunction(Array $data = array()) {
$data = array_merge($this->_defaults, $data);
$this->setData($data);
}
 
public function setData(Array $data = array()) {
foreach($data as $key => $value) {
$this->set($key, $value);
}
}
 
public function set($key, $value) {
$this->$key = $value;
return $this;
}
 
public function get($key) {
return $this->$key
}
}

I know that wasn't part of your question, but it's a related example.

your examples imply these 'parameters' are actually data values, that the function/method code will operate on as a set of same meaning data. in this case, you would pass them into the function as an array of data, so that the function can operate on any arbitrary amount of data without needing to ever alter the code definition or dynamically produce or dynamically call code, as the amount of data changes.

 

your second example also implies that you are hard-coding these values into a verbose amount of repetitive code, rather than use array functions to have simple general-purpose code operate on the set of data by looping over the data.

 

as an example of why you would NOT use individual parameters that represent same meaning data, just look at the php mysqli_stmt_bind_param() function and what it takes to use it with an arbitrary number of data values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.