Having this example code using the tertiary operator in PHP, I get the 'default value'.
$myObject = null; // I'm doing this for the example
$result = $myObject ? $myObject->path() : 'default value';
However, when I try to use the null coalescing operator to do this, I get the error
Call to a member function path() on null
$result = $myObject->path() ?? 'default value';
What am I doing wrong with the ?? operator?
These snippets do not work in the same way:
$result = $myObject ? $myObject->path() : 'default value';
says: use'default value'
if$myObject
isNULL
$result = $myObject->path() ?? 'default value';
says: use the default if$myObject->path()
returnsNULL
- but that call can only return something if the object itself is notNULL