I am confused about default values for PHP functions. Say I have a function like this:
function foo($blah, $x = "some value", $y = "some other value") {
// code here!
}
What if I want to use the default argument for $x and set a different argument for $y?
I have been experimenting with different ways and I am just getting more confused. For example, I tried these two:
foo("blah", null, "test");
foo("blah", "", "test");
But both of those do not result in a proper default argument for $x. I have also tried to set it by variable name.
foo("blah", $x, $y = "test");
I fully expected something like this to work. But it doesn't work as I expected at all. It seems like no matter what I do, I am going to have to end up typing in the default arguments anyway, every time I invoke the function. And I must be missing something obvious.
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.
Then call the function like so:
It is actually possible:
Whether you would want to do so is another story :)
UPDATE:
The reasons to avoid this solution are:
But it can actually be useful in situations where:
you could change the function but:
null
(or equivalent) is not an option (see DiegoDD's comment)func_num_args()
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.
I recently had this problem and found this question and answers. While the above questions work, the problem is that they don't show the default values to IDEs that support it (like PHPStorm).
if you use
null
you won't know what the value would be if you leave it blank.The solution I prefer is to put the default value in the function definition also:
The only difference is that I need to make sure they are the same - but I think that's a small price to pay for the additional clarity.
I would propose changing the function declaration as follows so you can do what you want:
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,
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:
This is case, when object are better - because you can set up your object to hold x and y , set up defaults etc.
Approach with array is near to create object ( In fact, object is bunch of parameters and functions which will work over object, and function taking array will work over some bunch ov parameters )
Cerainly you can always do some tricks to set null or something like this as default