Re: Combined assignment operator for short ternary

From: Date: Sat, 25 Aug 2012 17:49:35 +0000
Subject: Re: Combined assignment operator for short ternary
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-62493@lists.php.net to get a copy of this message
Am 25.08.2012 18:38, schrieb Ferenc Kovacs:
        would this trigger a notice if $foo is not defined?
        if yes, then it would be different from the current behavior of the
        ternary operator.
    Couldn't believe it, thus I tested it myself
snip
    Don't know, what you are talking about, but the notice _is_ the
    current behaiour and therefore: No difference.
Sorry, I messed up that email. What I wanted to say: If it accepts unset variable, then I could see usecases for it, but then it would behave differently than the current ternary. If it doesn't accept unset variable then it would in line with what we have, but I don't see any usecase for it, because I would have to set it before checking that it is falsely or not, in which case I would set it to the default if not set already.
        if no, then I would never use this.
        I mean if I have to set the variable before the check, then I
        would put
        the check into the assignment.
    The main thought about it was
    function foo ($bar = null) {
         $bar = $bar ?: 'default';
    }
If you wanted to enforce the 'default' value to be the default if no argument passed, then you could use $bar = 'default' in the method signature. So I guess that you use that construct to handle when the argument is passed, but it contains a falsely value ("0", array(), 0, etc.). I agree that this can be useful in some cases.
    I _always_ use 'null' as default
For me, it isn't always null, sometimes it is boolean true/false, or an empty array.
    - If you want to omit a parameter, but want to set one after that,
    you don't need to look whats the default: It's 'null'
my IDE takes care of that problem for me.
Only works on the green field ;) And/or if you are alone ...
    - Ive often enough seen something like
    function foo ($limit = 10) { /* code */ }
    // Somewhere else
    function bar ($limit = 50) { /* code */ $foo($limit); /* code */}
    // Even somewhere else
    bar();
same here.
Same here :p
-- Ferenc Kovács @Tyr43l - http://tyrael.hu


« previous php.internals (#62493) next »