Last active
December 25, 2015 12:49
-
-
Save marcelsud/6978750 to your computer and use it in GitHub Desktop.
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 | |
namespace Acme\Services; | |
class ServiceLocator { | |
private $services = array(); | |
public function set($name, $instance) | |
{ | |
$this->services[$name] = $instance; | |
} | |
public function get($name) | |
{ | |
return $this->services[$name]; | |
} | |
} | |
class SomeDatabaseMapper | |
{ | |
public function findAll($table) | |
{ | |
//Find all entries in some way | |
} | |
} | |
class MovieFinder | |
{ | |
private $container; | |
public function __construct(ServiceLocator $container) { | |
$this->container = $container; | |
} | |
public function findAll() | |
{ | |
return $this->container->get("db")->findAll("movies"); | |
} | |
} | |
class MovieLister | |
{ | |
private $container; | |
public function __construct(ServiceLocator $container) { | |
$this->container = $container; | |
} | |
public function list() | |
{ | |
return $this->container->get("movie_finder")->findAll(); | |
} | |
} | |
$container = new ServiceLocator(); | |
$container->set("db", new SomeDatabaseMapper()); | |
$container->set("movie_finder", new MovieFinder($container)); | |
$container->set("movie_lister", new MovieLister($container)); | |
$movieList = $container->get("movie_lister")->list(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Three words about it...
Avoid... avoid... avoid!