3

Possible Duplicate:
PHP - Calling functions with multiple variables

function test($var1=null, $var2=null, $var3=null){
    //smart stuff goes here
}

Do I have to every time call the function passing all variables?

test(null, $var2, null);

I'd like to pass only $var2 because all the other variables have default values... Is it even possible?

In JavaScript we can pass an object to the function, is there something similar in PHP?

Community
  • 1
  • 1
simPod
  • 11,498
  • 17
  • 86
  • 139
  • It's common to pass an array (PHP has associative arrays, no need for an object) as argument, containing the actual arguments. – Niko Jun 11 '12 at 16:22
  • I have done a research and found this interesting piece of code http://www.marco.org/2008/11/11/faking-named-parameters-in-php – simPod Jun 11 '12 at 16:32

1 Answers1

3

You only have to pass the arguments up to and including the last argument you do not wish to use the default value for. In your example, you could do this:

test(null, $var2);

The last argument can be omitted since the default value is satisfactory. But the first one must be included so PHP knows that you are setting the value for the second parameter.

Until PHP offers named parameters like Python does, this is how it has to work.

John Conde
  • 217,595
  • 99
  • 455
  • 496