NotionCommotion Posted November 1, 2014 Share Posted November 1, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292215-passing-parameters-as-variables-vs-passing-parameters-as-an-array/ Share on other sites More sharing options...
Alex_ Posted November 1, 2014 Share Posted November 1, 2014 (edited) 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 November 1, 2014 by Alex_ 1 Quote Link to comment https://forums.phpfreaks.com/topic/292215-passing-parameters-as-variables-vs-passing-parameters-as-an-array/#findComment-1495465 Share on other sites More sharing options...
mac_gyver Posted November 2, 2014 Share Posted November 2, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292215-passing-parameters-as-variables-vs-passing-parameters-as-an-array/#findComment-1495537 Share on other sites More sharing options...
NotionCommotion Posted November 2, 2014 Author Share Posted November 2, 2014 Thanks _Alex and mac_gyver, I've since changed to passing an array, and am very happy I did so. Quote Link to comment https://forums.phpfreaks.com/topic/292215-passing-parameters-as-variables-vs-passing-parameters-as-an-array/#findComment-1495548 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.