Real life example comparing a classic implementation (new
, singletons...)
VS using dependency injection.
Say you have:
class GoogleMaps
{
public function getCoordinatesFromAddress($address) {
// calls Google Maps webservice
}
}
class OpenStreetMap
{
public function getCoordinatesFromAddress($address) {
// calls OpenStreetMap webservice
}
}
The classic way of doing things is:
class StoreService
{
public function getStoreCoordinates($store) {
$geolocationService = new GoogleMaps();
// or $geolocationService = GoogleMaps::getInstance() if you use singletons
return $geolocationService->getCoordinatesFromAddress($store->getAddress());
}
}
Now we want to use the OpenStreetMap
instead of GoogleMaps
, how do we do?
We have to change the code of StoreService
, and all the other classes that use GoogleMaps
.
Without dependency injection, your classes are tightly coupled with their dependencies.
The StoreService
now uses dependency injection:
class StoreService {
private $geolocationService;
public function __construct(GeolocationService $geolocationService) {
$this->geolocationService = $geolocationService;
}
public function getStoreCoordinates($store) {
return $this->geolocationService->getCoordinatesFromAddress($store->getAddress());
}
}
And the services are defined using an interface:
interface GeolocationService {
public function getCoordinatesFromAddress($address);
}
class GoogleMaps implements GeolocationService { ...
class OpenStreetMap implements GeolocationService { ...
Now, it is for the user of the StoreService to decide which implementation to use.
The StoreService
is no longer tightly coupled with its dependency.
You may also see that dependency injection will leave with one drawback: you now have to handle injecting dependencies.
That's where a container, and specifically PHP-DI, can help you.
Instead of writing:
$geolocationService = new GoogleMaps();
$storeService = new StoreService($geolocationService);
You can write:
$storeService = $container->get('StoreService');
and configure which GeolocationService PHP-DI should automatically inject in StoreService through configuration:
// config/di.php
return [
'GeolocationService' => [
'class' => 'GoogleMaps',
],
];
If you change your mind, there's just one line of configuration to change now.
Wait!
What you saw is just a teeny tiny fraction of what PHP-DI can do.