$country = null;
if ($session !== null) {
$user = $session->user;
if ($user !== null) {
$address = $user->getAddress();
if ($address !== null) {
$country = $address->country;
}
}
}
/*Instead of null check conditions, you can now use a chain of calls with the
new nullsafe operator. When the evaluation of one element in the chain fails,
the execution of the entire chain aborts and the entire chain evaluates to null. */
// Example
$country = $session?->user?->getAddress()?->country;
$country = $session?->user?->getAddress()?->country;
The syntax is similar to the property/method access operator (->), and following the nullable type pattern, the null-safe operator is ?->.
$foo?->bar?->baz;
Null safe operator silently returns null if the expression to the left side evaluates to null.