Joshua Hutchison Joshua Hutchison - 9 months ago 40
PHP Question

How do I access the protected object returned by geocoder-php/GeocoderLaravel?

I installed the geocoder-php/GeocoderLaravel package and configured it according to the documentation. Then I ran a little test in a controller function and it went out to Google, resolved the address and returned the geocode object successfully, but it is protected and I can't access it. What's up with that?


$geocode = Geocoder::geocode('9900 Sowder Village Square, Manassas, VA 20109');


object(Geocoder\Result\Geocoded)#189 (15) {
["latitude":protected]=> float(38.7392838)
["longitude":protected]=> float(-77.5348982)
["bounds":protected]=> array(4) {
["south"]=> float(38.7392838)
["west"]=> float(-77.5348982)
["north"]=> float(38.7392838)
["east"]=> float(-77.5348982)
["streetNumber":protected]=> string(4) "9900"
["streetName":protected]=> string(21) "Sowder Village Square"
["cityDistrict":protected]=> NULL
["city":protected]=> string(8) "Manassas"
["zipcode":protected]=> string(5) "20109"
["county":protected]=> string(21) "Prince William County"
["countyCode":protected]=> string(21) "PRINCE WILLIAM COUNTY"
["region":protected]=> string(8) "Virginia"
["regionCode":protected]=> string(2) "VA"
["country":protected]=> string(13) "United States"
["countryCode":protected]=> string(2) "US"
["timezone":protected]=> NULL

I've searched the internet and found stuff about reflection, but I'm thinking it can't be that complicated. The guy's documentation on gitHub sucks. Any guidance?

Thanks for taking the time.



The property is protected because the API does not want you to touch the properties directly; they want use to use getters and setters.

getCoordinates() will return a Coordinates object (with latitude and longitude properties);

That's what you want.