0

If I have the following function:

function foo($a = 'a', $b = 'b', $c = 'c', $d = 'd')
{
    // Do something
}

Can I call this function and only pass the value for $d, therefore leaving all the other arguments with their defaults? With code something like this:

foo('bar');

Or do I have to call it with something like this:

foo(null, null, null, 'bar');
4
  • Possible duplicate of stackoverflow.com/questions/5968965/php-default-arguments Commented Sep 29, 2011 at 11:02
  • @Quasdunk, do you mean call it like foo('', '', '', 'bar')? Commented Sep 29, 2011 at 11:03
  • You have to pass all previous arguments, since PHP does not support named arguments as Python does. But see if this can help you Commented Sep 29, 2011 at 11:04
  • How would PHP know which argument you'd supplied? It's quite blatantly physically impossible. Commented Sep 29, 2011 at 11:05

6 Answers 6

1

No, you cannot. Use arrays:

function foo($args) {
   extract($args);
   echo $bar + $baz;
}

foo(array("bar" => 123, "baz" => 456));

Write a bug report on php.net and ask them to add named arguments to the language!

Sign up to request clarification or add additional context in comments.

2 Comments

Please don't write a bug report on php.net asking for named arguments as several have been written over the years, so any new bug would be a duplicate. There has been lots of discussion on the topic between the developers (even recently) with no decision to add this feature to PHP. However, do feel free to draft an RFC on the subject and discuss it (again) on the PHP internals mailing list.
absolutely true, unfortunately.
1

You have to use nulls like you said:

foo(null, null, null, 'bar');

If you don't mind creating more functions you could do something like this, I'd imagine the overall code would be neater.

function update_d($val){
    foo(null, null, null, $val);
}

Or you could use arrays like so:

$args = array($a = 'a', $b = 'b', $c = 'c', $d = 'd');
foo($args);

Comments

0

You have to do it like

foo(null, null, null, 'bar');

An alternative is to leave the arguments out of the function signature, and use func_get_args() to retrieve the values;

function foo() {
    $args = func_get_args();

But then still, if you would leave out the first three null values, there's no way to know that 'bar' is the $d parameter. Note by the way that this approach is undesirable most of the times, because it obfuscates your function signature and hurts performance.

Comments

0

Short answer: No. Long answer: Nooooooooooooooooooooooooooooo.

Default argument value will be used when the variable is not set at all.

Comments

0

You can't do it without overloading techniques.

In this case, your program assumes that if only one param is passed, it's the fourth.

Comments

-1

func_get_args() - Gets an array of the function's argument list.
func_num_args() - Returns the number of arguments passed to the function

function foo()
{
     $numargs = func_num_args();
     echo "Number of arguments: $numargs<br />\n";
}


foo(a); foo(1,b,c); will work fine

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.