0

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!

3 Answers 3

2

Take:

$city_google_api = $country->AdministrativeArea->Locality->LocalityName;

and temporarily re-write it:

$area            = $country->AdministrativeArea;
$locality        = $area->Locality;
$city_google_api = $locality->LocalityName;

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:

if (!is_object($country))
   throw SomeException();

$area = $country->AdministrativeArea;

if (!is_object($area))
   throw SomeException();

$locality = $area->Locality;

if (!is_object($locality))
   throw SomeException();

$city_google_api = $locality->LocalityName;

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.

Sign up to request clarification or add additional context in comments.

Comments

0
if (!is_object($country)) {
    //debug
}
if (!is_object($country->AdministrativeArea)) {
    //debug
}
//etc...

Comments

0

Actually there is not, but you can check if

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.