12

I have a PHP function, like this:

function($foo = 12345, $bar = false){}

What I want to do, is call this function with the default argument of $foo passed, but $bar set to true, like this (more or less)

function(DEFAULT_VALUE, true);

How do I do it? How do I pass an argument as a function's default value without knowing that value?

Thanks in advance!

arik
  • 28,170
  • 36
  • 100
  • 156
  • What I am thinking right now.. might there not also be a value like void? I mean, null is a value, but, at least in Java, the nothingness can be brought one step further, by using void. At least as the return value of a function, if it not only returns null, but actually nothing. – arik Jan 05 '11 at 16:04
  • there is no such "void" in PHP. `""` and `null` are identical – Pekka Jan 05 '11 at 17:12
  • Incredible that so many people on this page completely missed the point of the question (I thought it was quite clear). – SikoSoft Apr 24 '15 at 07:33

5 Answers5

12

This is not natively possible in PHP. There are workarounds like using arrays to pass all parameters instead of a row of arguments, but they have massive downsides.

The best manual workaround that I can think of is defining a constant with an arbitrary value that can't collide with a real value. For example, for a parameter that can never be -1:

define("DEFAULT_ARGUMENT", -1);

and test for that:

function($foo = DEFAULT_ARGUMENT, $bar = false){}
Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
3

put them the other way round:

function($bar = false, $foo = 12345){}

function(true);
Freddie
  • 1,717
  • 2
  • 16
  • 23
  • I can't, for I already use this function in a huge lot of places :D – arik Jan 05 '11 at 15:58
  • 2
    @arik-so refractor then, freddie's way the way to do it, that is exactly what i was going to answer – afarazit Jan 05 '11 at 19:31
  • usually you don't have at too many places those most extreme last parameters, and if you have, probably they should be on the more 'front' places – FantomX1 Sep 27 '19 at 19:50
3

The usual approach to this is that if (is_null($foo)) the function replaces it with the default. Use null, empty string, etc. to "skip" arguments. This is how most built-in PHP functions that need to skip arguments do it.

<?php
function($foo = null, $bar = false)
    {
    if (is_null($foo))
        {
        $foo = 12345;
        }
    }
?>
Core Xii
  • 6,270
  • 4
  • 31
  • 42
  • well, in my case, the argument being a string, null is an acceptable value ^^ Actually, in my case, more or less anything is an acceptable value. – arik Jan 05 '11 at 16:00
  • Ah, this is much better than my answer, I actually have used this before but I forgot about it. I'm sure some purists would argue with the approach though. – Tesserex Jan 05 '11 at 16:00
  • @arik-so you can do a type comparison as well to see if `NULL` is actually `NULL` and not an empty string (a la `===`) – Patrick Jan 05 '11 at 16:19
3

PHP can't do exactly that, so you'll have to work around it. Since you already need the function in its current form, just define another:

function blah_foo($bar)
{
    blah(12345, $bar);
}

function blah($foo = 12345, $bar = false) { }
Tesserex
  • 17,166
  • 5
  • 66
  • 106
  • that sounds interesting, too ^^ considering that any value is acceptable, I think your approach is the best. – arik Jan 05 '11 at 16:01
  • I'd be very careful with introducing new functions to the code for something like this, though. You need to document them, keep them in mind... It's very easy to build sub-optimal code this way – Pekka Jan 05 '11 at 16:40
-2

i think, it's better sorted based on the argument that the most frequently changed, $bar for example, we put it first,

function foo( $bar = false, $foo = 12345){
// blah blah...
}

so when you want to pass $bar to true, and $foo to 12345, you do this,

foo(true);
theHack
  • 1,938
  • 9
  • 26
  • 33