I have a function that takes 5 parameters, and as the application grew we needed to add few more parameters which ended up in 9 parameters with 4 of them having default values.
I was wondering is it better to pass parameters like this or use an array?
I prefer to have it like this
fun(array(
'par1' => 'x',
'par2' => 'y',
.....
)
)
Insted of
func($par1, $par2, $par3, ...);
What do you think?
Introduction to the PHP function parameters When a function has multiple parameters, you need to separate them using a comma ( , ). The concat() function has two parameters $str1 and $str2 . In this example, the $str1 will take the first argument 'Welcome ' , and the $str2 will take the second argument 'Admin' .
PHP Function Arguments To enter multiple arguments, you have to separate them by commas.
PHP Function ArgumentsArguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
But here are some solutions to this problem.
If the order is somewhat fixed and you never have a need to change it, then:
<?php
function fn($a1, $a2, $a3, $a4, $a5, $a6 = null, $a7 = "foo", $a8 = "bar", array $a9 = array()) {}
Pros
Cons
If on the other hand the order is somewhat different all the time, use the array thingy.
<?php
function fn($a1, $a2, $a3, $a4, $a5, array $optional = array()) {}
fn("", "", "", "", "", array("arg9" => false));
Pros
Cons
A parameter object is of course a valid solution as well, but impractical to handle:
<?php
class Args {
public $arg5 = "foo";
public $arg6 = "bar";
public $arg7 = null;
public $arg8 = array();
public $arg9 = true;
}
function fn($arg1, $arg2, $arg3, $arg4, $arg5, \Args $optional = null) {}
// Now comes the impractical part.
$optional = new Args();
$optional->arg9 = false;
fn("", "", "", "", "", $optional);
Pros
Cons
You could mix the two approaches:
<?php
class Args {
public $arg5 = "foo";
public $arg6 = "bar";
public $arg7 = null;
public $arg8 = array();
public $arg9 = true;
public __construct($args) {
foreach ($args as $property => $value) {
$this->"set{$property}"($value);
}
}
public function setArg5($value) {
if (is_string($value) === false) {
throw new \InvalidArgumentException;
}
$this->arg5 = $value;
}
// and so on ...
}
function fn($arg1, $arg2, $arg3, $arg4, $arg5, array $optional = null) {
if (isset($optional)) {
$optional = new Args($optional);
}
// ...
}
fn("", "", "", "", "", array("arg9" => false));
Pros
Cons
There's a new feature in PHP 5.6 that you might find useful, variadics:
<?php
function fn($a1, $a2, $a3, $a4, $a5, ...$optional) {}
Pros
Cons
And we might see named parameters in the future.
Passing 9 parameters to a function is a clear code smell:
Too many parameters: a long list of parameters in a procedure or function makes readability and code quality worse.
With that many parameters, there's also a good chance that you violate:
Long method: a method, function, or procedure that has grown too large.
Creating an arbitrary array
that groups together those parameters won't solve the real issue. It can make your code somewhat more readable and less vulberable to small problems like omitting a parameter ($arg1, $arg2, $arg4
), but not a real solution.
Figure out why the function needs that many parameters, then fix that issue.
There are a lot of techniques, reading a good article/book about (oo) code refactoring can give you some hints.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With