2

I would like to do something like this:

function readUser($aUser = loadDefaultUser()){

 //doing read User
}

I find that it will display a error to me, how can I pass a function return as a default value? Thank you.

DNB5brims
  • 29,344
  • 50
  • 131
  • 195

4 Answers4

5

I would rather give a Null value for this argument and then call loadDefaultUser() in the body of the function. Something like this:

function readUser($aUser = NULL){
    if(is_null($aUser)){
        $aUser = loadDefaultUser();
    }
    //...
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Alexis Métaireau
  • 10,767
  • 5
  • 24
  • 34
  • Parse error: syntax error, unexpected T_STRING, expecting '(' in /data/sandbox/g/7f2d9.php on line 3 – genesis Aug 13 '11 at 13:38
1

You can add a callback-parameter to your loadDefaultUser() function when it's finished it fires the callback function with the return/result. It's a bit like ajax-javascript callbacks.

function loadDefaultUser ( $callback ) 
{
   $result = true;       
   return $callback($result);
}
Micromega
  • 12,486
  • 7
  • 35
  • 72
1

Yes, you can provide a default argument. However, the default argument "must be a constant expression, not (for example) a variable, a class member or a function call."

You can fake this behaviour by using some constant value for the default, then replacing it with the results of a function call when the function is invoked.

We'll use NULL, since that's a pretty typical "no value" value:

function readUser($aUser = NULL) {
    if (is_null($aUser))
        $aUser = loadDefaultUser();

    // ... your code here
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Better use `if ($aUser === NULL)` as this is faster and saves PHP calling a (build-in) function `is_null()`. It doesn't really matter, but makes your PHP application faster "for free". – Shi Aug 13 '11 at 16:12
  • @Shi: (a) It's not any faster in practice; (b) avoid micro-optimisations like this; (c) hmm, it might be good for code symmetry though, since I set it to `NULL` just above. – Lightness Races in Orbit Aug 13 '11 at 16:41
  • Why? Why avoid using `=== NULL` instead of `is_null()`? And as I said, it doesn't really matter, still, you get that "boost" for free. So why not using it? Can you explain or give a link? Okay, regarding your code symmetry, so either be symmetric in `is_string()`, `is_int()`, `is_null()` or in `$a = NULL; if ($a === NULL) …`. Still, why (b)? – Shi Aug 13 '11 at 16:44
  • @Shi: Because `is_null` has a higher abstraction and is thus better. Though pretty unlikely here, as a matter of habit, if you use abstracted functions like that then you are protected in the future when the required behaviour changes. – Lightness Races in Orbit Aug 13 '11 at 16:47
  • 1
    Why avoid micro-optimisations? Lol. I don't even know where to _begin_. (That's not to say "avoid code that's the same as if you had micro-optimised". It means "avoid the act of micro-optimising.") – Lightness Races in Orbit Aug 13 '11 at 16:47
  • Begin *anywhere*? Or better, being with why using `=== NULL` is a micro-optimisation to avoid. In my book, there are two semantically equivalent constructs, so why not use the "faster" one? – Shi Aug 13 '11 at 16:49
0
function readUser($aUser = NULL){
   if ($aUser === NULL){
        $aUser = loadDefaultUser();
   }

   //do your stuff
}
genesis
  • 50,477
  • 20
  • 96
  • 125
  • 1
    the problem here is that you're testing for false, which is not a correct default value in many case. See my answer using NULL. – Alexis Métaireau Aug 13 '11 at 13:38
  • Apart from the fact that his code has wrong syntax it is better to use null, because false could be a valid input that should not trigger the default case. Best would be some kind of php internal function that returns wether the parameter was passed or initialized by default. – Nobody moving away from SE Aug 13 '11 at 13:43
  • @Nobody This can be done by using [func_num_args()](http://www.php.net/manual/en/function.func-num-args.php), but I doubt this style makes the code easier to maintain for anything but trivial argument lists. – Shi Aug 13 '11 at 16:16
  • @Shi: I meant something that I would call `was_passed_by_user` that will return true if the given variable was an argument that was passed by the user, false when it was initialized by default and NULL when the containing function has no argument of this name. – Nobody moving away from SE Aug 13 '11 at 16:32