Php 8.0 introduced the nullsafe operator which can be used like so $foo?->bar?->baz;
.
I have a code sample running on php 8.1 that throws the error Undefined property: stdClass::$first_name
even though it is using the nullsafe operator:
$reference = (object) $reference; // Cast of array to object
return [
'FirstName' => $reference?->first_name,
];
To solve the error, I have to use the null coalescing operator:
$reference = (object) $reference; // Cast of array to object
return [
'FirstName' => $reference->first_name ?? null,
];
Why is the nullsafe operator throwing an error in this case?