8

Possible Duplicate:
Any way to specify optional parameter values in PHP?

just randomly came across this.

If I have a function like so:

public function getSomething($orderBy='x', $direction = 'DESC', $limit=null){

//do something random

}

When calling the function is it possible to ignore the first two fields and leave them default yet specify the 3rd.

For example:

$random = $this->my_model->getSomething(USE_DEFAULT, USE_DEFAULT, 10);

I know I can pass the 1st and 2nd parameters but all im asking is if their is some kind of special keyword that just says use the default value.

Hope that makes sense. its not a problem, just curious.

thanks for reading

Community
  • 1
  • 1
fl3x7
  • 3,723
  • 6
  • 26
  • 37

4 Answers4

14

You need to do that yourself. You can use null to indicate that a default value should be used:

public function getSomething($orderBy = null, $direction = null, $limit = null) {
    // fallbacks
    if ($orderBy === null) $orderBy = 'x';
    if ($direction === null) $direction = 'DESC';

    // do something random
}

Then pass null when calling it to indicate that you want to use the defaults:

$random = $this->my_model->getSomething(null, null, 10);

Another possible solution that I use sometimes is an additional parameter at the very end of the parameter list, containing all optional parameters:

public function foo($options = array()) {
    // merge with defaults
    $options = array_merge(array(
        'orderBy'   => 'x',
        'direction' => 'DESC',
        'limit'     => null
    ), $options);

    // do stuff
}

That way you do not need to specify all optional arguments. array_merge() ensures that you are always dealing with a complete set of options. You would use it like this:

$random = $this->my_model->foo(array('limit' => 10));

It seems like there is no required parameter this particular case, but if you need one, simply add it in front of the optional ones:

public function foo($someRequiredParameter, $someOtherRequiredParameter, $options = array()) {
    // ...
}
jwueller
  • 30,582
  • 4
  • 66
  • 70
  • Syntactic offers an elegant approach to this. Check it here https://github.com/topclaudy/php-syntactic – cjdaniel Jun 19 '16 at 18:31
  • With PHP8 this can be neatly achieved with named parameters (https://php.watch/versions/8.0/named-parameters) `$random = $this->my_model->getSomething(limit: 10);` – Welder Lourenço Sep 29 '22 at 18:31
2

Honestly, this becomes a problem when functions are trying to do too much. There's almost always a better design pattern when you see a function grow to more than a few parameters (usually it's the poor guy that inherited the old code, and appending parameters is this quickest way to "get the job done").

Elusive's answer is the best according to your question, but take a look at cyclomatic complexity: http://en.wikipedia.org/wiki/Cyclomatic_complexity

This is a good way to know if your function is doing too much, which makes your question less of a problem than it probably is now.

landons
  • 9,502
  • 3
  • 33
  • 46
0

I don't think PHP can do this. The best solution that I know of is outlined here: http://www.php.net/manual/en/functions.arguments.php#70511

Daan
  • 3,403
  • 23
  • 19
-2

You cannot ignore params. But you can do something Like this:

public function getSomething($limit=null){

return $this->getSomething('x','DESC',$limit);

}


public function getSomething($orderBy='x', $direction = 'DESC', $limit=null){

...

}

see ya