【问题标题】:Access interface implemented method using interface object使用接口对象访问接口实现方法
【发布时间】:2018-02-26 08:34:23
【问题描述】:

我正在浏览依赖注入文档,我在下面看到了一些内容,在这段代码中,他们创建了一个 inreface 的对象,然后调用了实现的函数。我在这里感到困惑,这是依赖注入中可用的选项还是 opps 功能。

class StoreService {
private $geolocationService;

public function __construct(GeolocationService $geolocationService) 
{
    $this->geolocationService = $geolocationService;
}

public function getStoreCoordinates($store) {
    return $this->geolocationService->getCoordinatesFromAddress($store->getAddress());
}

}

interface GeolocationService {
    public function getCoordinatesFromAddress($address);
}


     //////////////////////////////////////////////////////

/////////////////////////////////////// /////////////// ///////////////////////////////////////// ////////////

class GoogleMaps implements GeolocationService 
{
    public function getCoordinatesFromAddress($address) {
        // calls Google Maps webservice
    }
}

class OpenStreetMap implements GeolocationService 
{
   public function getCoordinatesFromAddress($address) {
     // calls OpenStreetMap webservice
   }
}

【问题讨论】:

    标签: php interface


    【解决方案1】:

    这和 PHP-DI 无关,都是原生 PHP。这就是接口在 PHP 中的工作方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2013-01-24
      • 1970-01-01
      • 2013-08-16
      • 2011-05-01
      • 2023-03-04
      • 2019-01-08
      相关资源
      最近更新 更多