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!

3
  • 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. Commented Jan 5, 2011 at 16:04
  • there is no such "void" in PHP. "" and null are identical Commented Jan 5, 2011 at 17:12
  • Incredible that so many people on this page completely missed the point of the question (I thought it was quite clear). Commented Apr 24, 2015 at 7:33

5 Answers 5

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){}
Sign up to request clarification or add additional context in comments.

Comments

3

put them the other way round:

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

function(true);

3 Comments

I can't, for I already use this function in a huge lot of places :D
@arik-so refractor then, freddie's way the way to do it, that is exactly what i was going to answer
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
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;
        }
    }
?>

3 Comments

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.
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.
@arik-so you can do a type comparison as well to see if NULL is actually NULL and not an empty string (a la ===)
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) { }

2 Comments

that sounds interesting, too ^^ considering that any value is acceptable, I think your approach is the best.
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
-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);

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.