PHP|Trait实践

发布时间:2019-08-07 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了PHP|Trait实践脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

trait完成一部分接口的功能,同时也完成一部分父类的功能。

举个例子来说明traIT的需求场景:

当有两个class:RetailStore和Car类,这两个类,他们并不继承同一个父类,是两个不同的子类。现在有一个需求,需要这两个类都显示他们的经纬度属性。这时,两个类有了同样的功能:获取当前对象的经纬度属性。

解决这个问题有3个办法:

  1. 创建一个父类Geocodable,让两者都继承该类。然而这种方法并不好。因为这两个类确实是完全不相关的,并不应该继承同一祖先。

  2. 创建Geocodable接口,让这两个类均实现这两个接口。这种方法相对好一点,两个类均能保持自己的继承层次接口,只在当前的这两个类中实现这个接口,完成相同的功能。但是,我们要在这两个类中实现相同的功能,代码有重复,这样并不好。

最好的办法:创建Geocodabletrait,定义并实现经纬度相关方法,然后把在RetailStore和Car两个类中混入这个trait。这么做,即不会破坏继承层次结构,同时又实现复用。

创建Trait

<?php

trait MyTrait{}

定义trait:

<?php
trait Geocodable 
{
    protected $address;
    
    /** GeocoderGeocoder */
    protected $geocoder;
    
    protected $geocoderResult;
    
    public function setGeocoder(GeocoderGeocoderInterface $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->getLonnitude();
    }
    
    protected function geocodeAddress()
    {
        $this->geocoderResult = $this->geocoder->geocode($this->address);
        
        return true;
    }
} 

如何使用

<?php

class MyClass
{
    use MyTrait;
}
class RetailStore
{
    use Geocodable;
}

这样,每一个RetailStore类都可以使用Geocodable的特性了。

<?php

$geocoderAdapter = new GeocoderHttpAdapterCurlHttpAdapter();
$geocoderProvider = new GeocoderProviderGOOGLEMapsProvider($geocoderAdapter);
$geocoder = newGeocoderGeocoder($geocoderProvider);

$store = new RetailStore();
$store->setAddress('your set address');
$store->setGeocoder($geocoder);

$latitude = $store->getLatitude();
$longitude = $store->getLongitude();
echo $latitude, ':', $longitude;

参考

  1. Modern PHP

脚本宝典总结

以上是脚本宝典为你收集整理的PHP|Trait实践全部内容,希望文章能够帮你解决PHP|Trait实践所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。