Skip to content

Instantly share code, notes, and snippets.

@juriansluiman
Created March 31, 2013 13:25
Show Gist options
  • Save juriansluiman/5280585 to your computer and use it in GitHub Desktop.
Save juriansluiman/5280585 to your computer and use it in GitHub Desktop.
Dependency injection versus Service Locator pattern
<?php
// Dependency
class Dependency
{
}
// Service
class Service
{
public function __construct(Dependency $dependency) {}
}
// Factory
$sl->setService('dependency', new Dependency);
$sl->setFactory('service', function($sl) {
$dependency = $sl->get('dependency');
$service = new Service($dependency);
return $service;
});
<?php
// Dependency
class Dependency
{
}
// Service
class Service
{
public function __construct(ServiceLocator $sl)
{
$dependency = $sl->get('dependency');
}
}
// Factory
$sl->setService('dependency', new Dependency);
$sl->setFactory('service', function($sl) {
$service = new Service($sl);
return $service;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment