passing in arrays to your functions rather than using variables
<p>this is probably an old technique, but i discovered it the other day when i was playing around, and i thought wow i should do this to all my functions, i cant see the downside, apart from having to remember the array types that are needed, plus you could add defaults if needed anyway to remember them down the track. </p>
<p>Anyway here is my example function before adding the array.</p>
<h4>PHP</h4>
<pre><code class="php hljs">$listallresult = $myclass->listall(<br /> $start = 0,<br /> $max = 50,<br /> $listtype = "latest",<br /> $templatefile = "list-item.html",<br /> $searchterm = ""<br /> );</code></pre>
<p>the array is now layed out like the following</p>
<h4>PHP</h4>
<pre><code class="php hljs"> $listoptions = [<br /> "start" => 0,<br /> "max" => 50,<br /> "listtype" => "latest",<br /> "templatefile" => "list.html",<br /> "searchterm" => "",<br /> ];</code></pre>
<p>and you just pass the array into the function</p>
<p>So rather than calling the funciton with all the variables in it you can just change the parts of the array 1st and then call the function.</p>
<h4>PHP</h4>
<pre><code class="php hljs">$listoptions["listtype"] = "latest";</code></pre>
<p>then run the listall function with the options</p>
<h4>PHP</h4>
<pre><code class="php hljs">$importslistall = $imports->list();</code></pre>
<p>to get the values in the function just loop through the array and assign all to variables</p>
<h4>PHP</h4>
<pre><code class="php hljs"> foreach ($this->listoptionsarray as $optionsname => $optionsvalue) {<br /> / ${$optionsname} = $optionsvalue; /<br /> ${$optionsname} = $this->db->escapeString($optionsvalue); // escape all incoming data<br /> $bugs .= "$$optionsname: $options_value<br />";<br /> }</code></pre>
<p>makes your code a bit more complex, but i think its valuable</p>
<p> </p>
