Because getAddress() is a string? How will you call getCountry() on a string?
Sep 17, 2021
5
Level 5
Nullsafe operator
I am learning about nullsafe operator. I have written the below code:
<?php
class User
{
public $isoCode = '259874';
public function getAddress() : string {
return 'Dhaka';
}
public function getCountry() : string {
return 'Bangladesh';
}
}
$user = new User();
echo $user->getAddress()?->getCountry()?->isoCode;
But why this getAddress() giving me this error?
Fatal error: Uncaught Error: Call to a member function getCountry() on string in C:\xampp\htdocs\php8\nullsafe.php:18 Stack trace: #0 {main} thrown in C:\xampp\htdocs\php8\nullsafe.php on line 18
Please or to participate in this conversation.