Using Default Arguments in a Function Using Default Arguments in a Function php php

Using Default Arguments in a Function


I would propose changing the function declaration as follows so you can do what you want:

function foo($blah, $x = null, $y = null) {    if (null === $x) {        $x = "some value";    }    if (null === $y) {        $y = "some other value";    }    code here!}

This way, you can make a call like foo('blah', null, 'non-default y value'); and have it work as you want, where the second parameter $x still gets its default value.

With this method, passing a null value means you want the default value for one parameter when you want to override the default value for a parameter that comes after it.

As stated in other answers,

default parameters only work as the last arguments to the function.If you want to declare the default values in the function definition,there is no way to omit one parameter and override one following it.

If I have a method that can accept varying numbers of parameters, and parameters of varying types, I often declare the function similar to the answer shown by Ryan P.

Here is another example (this doesn't answer your question, but is hopefully informative:

public function __construct($params = null){    if ($params instanceof SOMETHING) {        // single parameter, of object type SOMETHING    } elseif (is_string($params)) {        // single argument given as string    } elseif (is_array($params)) {        // params could be an array of properties like array('x' => 'x1', 'y' => 'y1')    } elseif (func_num_args() == 3) {        $args = func_get_args();        // 3 parameters passed    } elseif (func_num_args() == 5) {        $args = func_get_args();        // 5 parameters passed    } else {        throw new \InvalidArgumentException("Could not figure out parameters!");    }}


Optional arguments only work at the end of a function call. There is no way to specify a value for $y in your function without also specifying $x. Some languages support this via named parameters (VB/C# for example), but not PHP.

You can emulate this if you use an associative array for parameters instead of arguments -- i.e.

function foo(array $args = array()) {    $x = !isset($args['x']) ? 'default x value' : $args['x'];    $y = !isset($args['y']) ? 'default y value' : $args['y'];    ...}

Then call the function like so:

foo(array('y' => 'my value'));


It is actually possible:

foo( 'blah', (new ReflectionFunction('foo'))->getParameters()[1]->getDefaultValue(), 'test');

Whether you would want to do so is another story :)


UPDATE:

The reasons to avoid this solution are:

  • it is (arguably) ugly
  • it has an obvious overhead.
  • as the other answers proof, there are alternatives

But it can actually be useful in situations where:

  • you don't want/can't change the original function.

  • you could change the function but:

    • using null (or equivalent) is not an option (see DiegoDD's comment)
    • you don't want to go either with an associative or with func_num_args()
    • your life depends on saving a couple of LOCs

About the performance, a very simple test shows that using the Reflection API to get the default parameters makes the function call 25 times slower, while it still takes less than one microsecond. You should know if you can to live with that.

Of course, if you mean to use it in a loop, you should get the default value beforehand.