Jump to content

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.

Edited by Alex_
  • Like 1

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.