24

Let's say I've got a PHP function foo:

function foo($firstName = 'john', $lastName = 'doe') {
    echo $firstName . " " . $lastName;
}
// foo(); --> john doe

Is there any way to specify only the second optional parameter?

Example:

foo($lastName='smith'); // output: john smith
0

13 Answers 13

34

PHP does not support named parameters for functions per se. However, there are some ways to get around this:

  1. Use an array as the only argument for the function. Then you can pull values from the array. This allows for using named arguments in the array.
  2. If you want to allow optional number of arguments depending on context, then you can use func_num_args and func_get_args rather than specifying the valid parameters in the function definition. Then based on number of arguments, string lengths, etc you can determine what to do.
  3. Pass a null value to any argument you don't want to specify. Not really getting around it, but it works.
  4. If you're working in an object context, then you can use the magic method __call() to handle these types of requests so that you can route to private methods based on what arguments have been passed.
2
  • Although there is no implicit support, there are better workarounds than the accepted answer suggests. See Walf's solution below. There is more detail if you need it in my solution (which I swear I arrived at independently!)
    – DJDave
    Dec 15, 2015 at 7:58
  • Php8 now supports named parameters.
    – Anther
    Oct 26, 2021 at 18:11
23

A variation on the array technique that allows for easier setting of default values:

function foo($arguments) {
  $defaults = array(
    'firstName' => 'john',
    'lastName' => 'doe',
  );

  $arguments = array_merge($defaults, $arguments);

  echo $arguments['firstName'] . ' ' . $arguments['lastName'];
}

Usage:

foo(array('lastName' => 'smith')); // output: john smith
1
  • 2
    For associative arrays, $arguments += $defaults; is cleaner.
    – Walf
    Dec 11, 2015 at 0:56
9

You could refactor your code slightly:

function foo($firstName = NULL, $lastName = NULL)
{
    if (is_null($firstName))
    {
        $firstName = 'john';
    }
    if (is_null($lastName ))
    {
        $lastName = 'doe';
    }

    echo $firstName . " " . $lastName;
}

foo(); // john doe
foo('bill'); // bill doe
foo(NULL,'smith'); // john smith
foo('bill','smith'); // bill smith
0
6

If you have multiple optional parameters, one solution is to pass a single parameter that is a hash-array:

function foo(array $params = array()) {
    $firstName = array_key_exists("firstName", $params) ?
      $params["firstName"] : "";
    $lastName = array_key_exists("lastName", $params) ?
      $params["lastName"] : "";
    echo $firstName . " " . $lastName;
}

foo(['lastName'=>'smith']);

Of course in this solution there's no validation that the fields of the hash array are present, or spelled correctly. It's all up to you to validate.

0
3

No. The usual way of doing this is with some heuristics to determine which parameter was implied, like string length, typing, etc.

Generally speaking, you'd write the function to take the parameters in the order of most required to least required.

1
  • Heuristics only work reliably when parameters are unmistakable by type, like jQuery does. Other methods, like string length, is an accident waiting to happen.
    – Walf
    Apr 11, 2014 at 6:56
3

The way you want: no.

You could use some special mark, like NULL to note that value is not supplied:

function foo($firstName, $lastName = 'doe') {
    if (is_null($firstName))
        $firstName = 'john';
    echo $firstName . " " . $lastName;
}

foo(null, 'smith');
3

PHP 8 introduced named arguments so now it's possible to specify the parameters passed to a function.

before PHP 8:

htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);

after PHP 8:

htmlspecialchars($string, double_encode: false);

Reference

2

I wish this solution had been on SO when I started using PHP 2.5 years ago. It works great in the examples I have created, and I don't see why it shouldn't be thoroughly extensible. It offers the following benefits over those proposed previously:

(i) all access to parameters within the function is by named variables, as if the parameters were fully declared, rather than requiring array access

(ii) it is very quick and easy to adapt existing functions

(iii) only a single line of additional code is required for any function (in addition to the inescapable necessity of defining your default parameters, which you would be doing in the function signature anyway, but instead you define them in an array). Credit for the additional line is wholly due to Bill Karwin. This line is identical for every function.

Method

Define your function with its mandatory parameters, and an optional array

Declare your optional parameters as local variables

The crux: replace the pre-declared default value of any optional parameters using those you have passed via the array.

extract(array_merge($arrDefaults, array_intersect_key($arrOptionalParams, $arrDefaults)));

Call the function, passing its mandatory parameters, and only those optional parameters that you require

For example,

function test_params($a, $b, $arrOptionalParams = array()) {

$arrDefaults = array('c' => 'sat',

                     'd' => 'mat');

extract(array_merge($arrDefaults, array_intersect_key($arrOptionalParams, $arrDefaults)));

echo "$a $b $c on the $d";

}

and then call it like this

test_params('The', 'dog', array('c' => 'stood', 'd' => 'donkey'));
test_params('The', 'cat', array('d' => 'donkey'));
test_params('A', 'dog', array('c' => 'stood'));

Results:

The dog stood on the donkey

The cat sat on the donkey

A dog stood on the mat

2
  • Acknowledged - I saw it two weeks ago and gave you a point. I confess I didn't re-read all the "solutions" before I posted mine. If I'm allowed I will add a comment to the accepted answer pointing to your solution.
    – DJDave
    Dec 15, 2015 at 7:55
1

There are a few 'options' style implementations mentioned here. None thus far are very bulletproof if you plan to use them as as standard. Try this pattern:

function some_func($required_parameter, &$optional_reference_parameter = null, $options = null) {
    $options_default = array(
        'foo' => null,
    );
    extract($options ? array_intersect_key($options, $options_default) + $options_default : $options_default);
    unset($options, $options_default);

    //do stuff like
    if ($foo !== null) {
        bar();
    }
}

This gives you function-local variables (just $foo in this example) and prevents creating any variables that do not have a default. This is so no one can accidentally overwrite other parameters or other variables within the function.

1

Arguments need to be passed in order by position, you cannot skip a parameter per se; you'll have to supply the default parameter value in order to skip it. Arguably that defeats the purpose of what you're trying to achieve.

Without rewriting your function to accept parameters differently, here's a call-time way to work around this:

$func = 'foo';
$args = ['lastName' => 'Smith'];

$ref = new ReflectionFunction($func);
$ref->invokeArgs(array_map(function (ReflectionParameter $param) use ($args) {
    if (array_key_exists($param->getName(), $args)) {
        return $args[$param->getName()];
    }
    if ($param->isOptional()) {
        return $param->getDefaultValue();
    }
    throw new InvalidArgumentException("{$param->getName()} is not optional");
}, $ref->getParameters()));

In other words, you're using reflection to inspect the function's parameters and map them to the available parameters by name, skipping optional parameters with their default value. Yes, this is ugly and cumbersome. You could use this sample to create a function like:

call_func_with_args_by_name('foo', ['lastName' => 'Smith']);
0
0

If this is used very often, just define a new specialized function :

function person($firstName = 'john', $lastName = 'doe') {
    return $firstName . " " . $lastName;
}

function usualFirtNamedPerson($lastName = 'doe') {
    return person('john', $lastName);
}

print(usualFirtNamedPerson('smith')); --> john smith

Note that you could also change the default value of $lastname in the process if you wish.

When a new function is estimated overbloated, just call you function with all parameters. If you want to make it more clear, you can prestore your literals in fin named variable or use comments.

$firstName = 'Zeno';
$lastName = 'of Elea';
print(person($firstName, $lastName));
print(person(/* $firstName = */ 'Bertrand', /* $lastName = */ 'Russel'));

Ok, this is not as short and elegant as person($lastName='Lennon'), but it seems you can't have it in PHP. And that's not the sexiest way to code it, with super metaprogramming trick or whatever, but what solution would you prefer to encounter in a maintenance process?

0

Sadly what you're trying to do has no "syntactic sugar" way of doing it. They're all varying forms of WTF.

If you need a function that takes an undefined number of arbitrary parameters,

function foo () { 
     $args = func_get_args(); 
     # $args = arguments in order 
}

Will do the trick. Try avoid using it too much, because for Php this is a bit on the magical side.

You could then optionally apply defaults and do strange things based on parameter count.

function foo() { 
   $args = func_get_args();
   if (count($args) < 1 ) { 
       return "John Smith"; 
   }
   if (count($args) < 2 ) { 
       return "John " . $args[0];
   }
   return $args[0] . " " . $args[1];
}

Also, you could optionally emulate Perl style parameters,

function params_collect($arglist){ 
    $config = array();
    for ($i = 0; $i < count($arglist); $i+=2) { 
        $config[$i] = $config[$i+1];
    }
    return $config; 
}
function param_default($config, $param, $default){ 
    if (!isset($config[$param])) { 
        $config[$param] = $default;
    }
    return $config;
}

function foo() { 
   $args = func_get_args();
   $config = params_collect($args); 
   $config = param_default($config, 'firstname', 'John'); 
   $config = param_default($config, 'lastname', 'Smith'); 
   return $config['firstname'] . ' ' . $config['lastname'];   
}

foo('firstname', 'john', 'lastname', 'bob'); 
foo('lastname', 'bob', 'firstname', 'bob'); 
foo('firstname', 'smith'); 
foo('lastname', 'john');

Of course, it would be easier to use an array argument here, but you're permitted to have choice ( even bad ways ) of doing things.

notedly, this is nicer in Perl because you can do just foo( firstname => 'john' );

0
-1

No there isn't but you could use an array:

function foo ($nameArray) {
    // Work out which values are missing?
    echo $nameArray['firstName'] . " " . $nameArray['lastName'];
}

foo(array('lastName'=>'smith'));
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.