<?phptrait geocodable{/** @var String*/ protected $address; /** @var \geocoder\geocoder*/ protected $geocoder; /** @var \geocoder\result\geocoded*/ protected $geocoderResult; Public functionSetgeocoder (\geocoder\geocoderinterface$gocoder) { $this->geocoder =$gocoder; } Public functionSetaddress ($address) { $this->address =$address; } Public functionGetlatitude () {if(isset($this->geocoderresult) = =false){ $this-geocodeaddress (); } return $this->geocoderresult->Getlatitude (); } Public functionGetlongitude () {if(isset($this->geocoderresult) = = =false){ $this-geocodeaddress (); } return $this->geocoderresult->Getlongitude (); } protected functiongeocodeaddress () {$this->geocoderresult =$this->geocoder->geocode ($this-address); return true; }}//Use traitsclassretailstore{ Usegeocodable; //here is the implementation of the class}$geocoderAdapter=New\geocoder\httpadapter\curlhttpadapter ();$geocoderProvider=New\geocoder\provider\googlemapsprovider ($geocoderAdapter);$geocoder=New\geocoder\geocoder ($geocoderProvider);$store=NewRetailstore ();$store->setaddress (' 420 9th Avenue, New York, NY 10001 USA ');$store->setgeocoder ($gocoder);$latitude=$store-getlatitude ();$longitude=$store-getlongitude ();Echo $latitude, ‘:‘,$longitude;
Use of PHP traits