Am 24.08.2012 23:11, schrieb Ferenc Kovacs:
On Fri, Aug 17, 2012 at 11:41 PM, Sebastian Krebs <krebs....@gmail.com
<mailto:krebs....@gmail.com>> wrote:
Hi,
Don't know, how complicated this is (and also someone (not me) must
implement it, because I can't :X), but to be in sync with the
operators the short ternary operator should be usable in conjunction
with the assignment like the other binary operators. Don't know, if
anybody understands me :D So here is an example
// instead of
$foo = $foo ?: 'default';
// Just
$foo ?:= 'default';
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
$ php -a
Interactive mode enabled
<?php
$f = $f ?: 'bar';
PHP Notice: Undefined variable: f in - on line 2
PHP Stack trace:
PHP 1. {main}() -:0
Notice: Undefined variable: f in - on line 2
Call Stack:
8.9779 619512 1. {main}() -:0
Don't know, what you are talking about, but the notice _is_ the current
behaiour and therefore: No difference.
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';
}
I _always_ use 'null' as default
- 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'
- 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();
Imagine there are some other methods and functions in between it's quite
annoying.
--
Ferenc Kovács
@Tyr43l - http://tyrael.hu
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php