4

I have a search function that queries the database and has ~15 optional parameters. Obviously this is not pretty and calling it is a bit of a mess. PHP does not allow overloading methods so I've just been creating huge function signatures.

Elsewhere I've seen suggestions such as creating a parameter class: Disadvantages of using a lot of parameters

But this seems too heavy. I could pass in an associative array, but while this reduces the number of parameters I believe it is less easy to follow as there is no built in documentation stating what keys should exist in the array.

Is there any other way to handle this gracefully? Typically in other languages I would have a really ugly private method that takes up to a dozen parameters and then create public methods of the same name which accept a subset of those parameters and internally call the private method.

Community
  • 1
  • 1
aw crud
  • 8,791
  • 19
  • 71
  • 115

6 Answers6

4

In PHP, you can use associative array:

someFunction(array(
    "a" => 3243,
    "b" => 2354,
    "c" => 33453,
    "d" => 324353,
    "e" => 321243,
    "f" => 321243,
    "g" => 312243,
    "h" => 321243,
))

Or properties of the object that the function is being called on (if it makes sense). PHPMailer send mails like this:

// instantiate the class
$mailer = new PHPMailer();

// Set the subject
$mailer->Subject = 'This is a test';

// Body
$mailer->Body = 'This is a test of my mail system!';

// Add an address to send to.
$mailer->AddAddress('foo@host.com', 'Eric Rosebrock');

if(!$mailer->Send())
{
    echo 'There was a problem sending this mail!';
}

And it has many more optional parameters. It could as well use a method with hundred parameters, but this is much more readable.

EDIT: These solutions also better support optional parameters. In case of properties it is straightforward, in case of associative array, you can merge the array with array of default values.

Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
  • Using an array as a parameter has also the advantage of not forcing you to make a hierarchy of arguments. And at the start of your function you can still check if required arguments are given. – Nabab Feb 08 '11 at 13:22
  • Yes, but then there is no meaningful documentation in the function signature -- how is somebody to know which parameters are allowed unless I were to separately document that and keep it up to date? – aw crud Feb 08 '11 at 13:23
  • Good place to do this is a Doxygen/PHPDoc comment (google these if you dont know them). Advanced IDEs will then be able to show you such documentation while typing the code. – Matěj Zábský Feb 08 '11 at 13:25
2

In general the long parameter list is a so called bad smell in code which can be removed via refactoring called Introduce parameter object. See this for reference.

Cheeres

1

Yes, the good rule of thumb is to have no more than 3-4 params. If you need more then normally you should use array or object as one of the params. But in some cases if you think you really need more params, then sure, why not. If it makes your code easy to understand and use, then why not.

Dmitri
  • 34,780
  • 9
  • 39
  • 55
0

You could create a class which stores the parameters as properties, allowing you to set each property as you need, then have a method which uses these properties to query the database. The constructor can set default values for these properties. This just makes calling a bit easier.

$function = ClassFunction();
$function->arg1 = 'Some value.';
$function->arg2 = true;
$function->arg3 = 5;

$result = $function->call_method();    // This uses default values for any property not set.
0

The problem "too many parameters" in my opinion is only the manifestation of a much deeper underlaying problem: a bad architechture. If a function really needs all those parameter values, chances are very hight that it is doing a lot more than it should.

This should be a reminder "oh hey, let's reconsider not using procedure X to do all the stuff but to think what should really be done by X and what should be done by Y and Z.

Christian Seifert
  • 2,780
  • 5
  • 29
  • 45
0

It'd be nice to convert your function to class. There are two major advantages:

  • Function arguments converted to properties and can be commented

  • Function code which I think is rather large can be split into set of smaller private methods

Yuri Subach
  • 301
  • 1
  • 4