I’m using the Google Map API V3 sending it the Lat/Lng and it returns the full address. I’m parsing the address that’s returned by using the Object. It works for many addresses but for some of them it is generating the PHP error of about the property being a non-object:
PHP Notice: Trying to get property of non-object in get5.php on line 47
PHP Notice: Trying to get property of non-object in get5.php on line 48
PHP Notice: Trying to get property of non-object in get5.php on line 49
Here are the lines of code for 47-49:
$city_google_api = $country->AdministrativeArea->Locality->LocalityName;
$state_google_api = $country->AdministrativeArea->AdministrativeAreaName;
$zip_code_google_api = $country->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
I would like to debug this further, but I don’t know which record this is causing these messages regarding the property of non-object. Is there a way to trap on the error or check for “property of non-object” so that I can dump the variables? I don’t want to dump this for each record because that’s too much output. In general, I would like to know if there is a way to trap errors to dump variables? Thanks!
Take:
and temporarily re-write it:
Do the same for the other three lines and, presto, the line number in the error will tell you precisely where the problem is.
Divide and conquer — your debug technique friend.
You could also program defensively from the start so that the error is not just better reported, but “caught” at runtime:
or go further and examine the actual type of each object. This is usually overkill, though — once you have fixed your code, you will know what type each variable has, and the original three lines will suffice.