很多php開發朋友都沒有弄清楚Trait(性狀)。這是PHP5.4.0引入的新概念,既像類又像接口。性狀是類的補分實現(即常量、屬性、方法),可以混入一個或者多個現有的PHP類中。 性狀有兩個作用:表明類可以做什么(像是接口);提供模塊化實現(像是類)。 PHP使用一種典型的繼承模型,在這種模型中,我們先編寫一個通用的根類,實現基本功能,然后擴展這個根類,創建更具體的類,從直接父類繼承實現。這叫繼承層次結構,很多編程語言都使用了這個模式 大多時候,這種典型的繼承模型能良好的運行,可是,如果想讓兩個無關的PHP類具有類似的行為,應該怎么做呢?
假如Shop和Car兩個PHP類的作用十分不同,而且在繼承層次結構中沒有共同的父類。 那么這兩個類都應該能使用地理編碼技術轉化成經緯度,然后在地圖上顯示。要怎么解決這個問題呢?
創建一個父類讓Shop和Car都繼承它 創建一個接口,定義實現地理編碼功能需要哪些方法,然后讓Shop和Car兩個類都實現這個接口 創建一個性狀,定義并實現地理編碼相關的方法,然后把在Shop和Car兩個類中混入這個性狀 第一種解決方法不好,因為我們強制讓兩個無關的類繼承同一個祖先,而很明顯,這個祖先不屬于各自的繼承層次結構。
第二種解決方法好,因為每個類都能保有自然的繼承層次結構。但是,我們要在兩個類中重復實現的地理編碼功能,這不符合DRY原則。 注:DRY是 Don’t Repeat Yourself(不要自我重復)的簡稱,表示不要在多個地方重復編寫相同的代碼,如果需要修改遵守這個原則編寫的代碼,只需要在一出修改,改動就能體現到其他地方。
第三種解決方法最好,這么做不會攪亂這兩個類原本自然的繼承層次結構。
如何創建性狀?
trait Geocodable { // 這里是性狀的實現}如何使用性狀?
class Shop{ use Geocodable; // 這里是類的實現}例子 創建一個Geocodable 性狀
trait Geocodable { /** * 地址 * @var string */ PRotected $address; /** * 編碼器對象 * @var /Geocoder/Geocoder */ protected $geocoder; /** * 處理后結果對象 * @var /Geocoder/Result/Geocoded */ protected $geocoderResult; //注入Geocoder對象 public function setGeocoder(/Geocoder/GeocoderInterface $geocoder) { $this->geocoder = $geocoder; } //設定地址 public function setAddress($address) { $this->address = $address; } //返回緯度 public function getLatitude() { if (isset($this->geocoderResult) === false) { $this->geocodeAddress(); } return $this->geocoderResult->getLatitude(); } //返回經度 public function getLongitude() { if (isset($this->geocoderResult) === false) { $this->geocodeAddress(); } return $this->geocoderResult->getLongitude(); } //把地址字符串傳給Geocoder實例,獲取經地理編碼器處理得到的結果 protected function geocodeAddress() { $this->geocoderResult = $this->geocoder->geocode($this->address); return true; }}使用性狀
$geocoderAdapter = new /Geocoder/HttpAdapter/CurlHttpAdapter();$geocoderAdapter = new /Geocoder/Provider/GoogleMapsProvider($geocoderAdapter);$geocoder = new /Geocoder/Geocoder($geocoderProvider);$store = new Shop();$store->setAddress('420 9th Avenue, New York, NY 10001 USA');$store->setGeocoder($geocoder);$latiude = $store->getLatitude();$longitude = $store->getLongitude();echo $latitude, ':', $longitude;新聞熱點
疑難解答
圖片精選