使用接口对象的Access接口实现方法

问题描述 投票:-1回答:1

我正在浏览依赖注入文档,我已经看到下面的内容,在这段代码中,他们创建了一个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个回答
0
投票

这与PHP-DI无关,这都是本机PHP。这就是接口在PHP中的工作方式。

© www.soinside.com 2019 - 2024. All rights reserved.