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.

David

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:
https://3v4l.org/RlDjK

Are you suggesting that it should emit a warning if `$var` is undefined,
as it does in this case? https://3v4l.org/aZAj0

Cheers,
Ben


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to