Created
March 31, 2013 13:25
-
-
Save juriansluiman/5280585 to your computer and use it in GitHub Desktop.
Dependency injection versus Service Locator pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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