Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP function - ignore some default parameters [duplicate]

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

like image 841
fl3x7 Avatar asked Mar 02 '12 23:03

fl3x7


People also ask

What is $params in PHP?

PHP Parameterized functions They are declared inside the brackets, after the function name. A parameter is a value you pass to a function or strategy. It can be a few value put away in a variable, or a literal value you pass on the fly. They are moreover known as arguments.

How do we define default function parameters in PHP?

The default parameter concept comes from C++ style default argument values, same as in PHP you can provide default parameters so that when a parameter is not passed to the function. Then it is still available within the function with a pre-defined value. This function also can be called optional parameter.

Does PHP support variable length arguments?

PHP supports variable length argument function. It means you can pass 0, 1 or n number of arguments in function. To do so, you need to use 3 ellipses (dots) before the argument name. The 3 dot concept is implemented for variable length argument since PHP 5.6.


1 Answers

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()) {
    // ...
}
like image 125
jwueller Avatar answered Oct 14 '22 05:10

jwueller