Re: [PHP-DEV] Re: New operator suggestion

September 4, 2021 22:56 (=?UTF-8?Q?David_Kol=c3=a1=c5=99_-_Micropro_Software?=)
Well, yes, but not exactly.

What I dislike on ?? operator is, that it supress all warnings in the 
expression - in my opinion it shares the same
issue as @ operator and why people discourage other to use it - because 
it may supress far more errors than you want
to supress. And this is the case with $var->prop1->prop2 - when I expect 
just prop2 being undefined, it supresses even
error of undefined $var, which might not be intentional and I miss that 
error because of that. So suggested ??: operator
should behave like ??, but without the supression mechanic - just short 
hand for
"$x === null ? $x : expr" instead of "isset($x) ? $x : expr".

The 2nd case you wrote is actually even weird to me - I am not sure at 
all if PHP should behave like that. Because in
your example, when $var is undefined:

$var->prop1->prop2 ?? ''
does not emit any error, because ?? supresses them

$var?->prop1?->prop2 ?? ''
does emit error - which is weird, since ?-> is still wrapped under ??, 
so I am not sure why error shows here.

Anyway what I want is emitting error on undefined $var and undefined 
prop1, but NOT undefined prop2, when:
$var->prop1?->prop2 ??: ''

Do you get it? Just a shorthand for this:
$var->prop1?->prop2 === null ? $var->prop1->prop2 : '';

Which any of suggested current solutions can't do.


On 05.09.2021 0:42, Ben Ramsey wrote:
> David Kolář wrote on 9/4/21 17:19: >> Back to the suggestion - I suggest creating a new IFNULL operator, which >> will simply test if >> expression is null. If not, it returns left-hand part, if yes, it >> returns right-hand part. > This is already what the `??` operator does. For example: > > > Are you suggesting that it should emit a warning if `$var` is undefined, > as it does in this case? > > Cheers, > Ben >