Last active
December 25, 2015 12:59
-
-
Save marcelsud/6980103 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 SomeDatabaseMapper | |
{ | |
public function findAll($table) | |
{ | |
//Find all entries in some way | |
} | |
} | |
class Container { | |
private $services = array(); | |
public function set($name, $instance) | |
{ | |
$this->services[$name] = $instance; | |
} | |
public function get($name) | |
{ | |
return $this->services[$name]; | |
} | |
} | |
class MovieFinder | |
{ | |
private $db; | |
public function __construct(SomeDatabaseMapper $db) { | |
$this->db = $db; | |
} | |
public function findAll() | |
{ | |
return $this->db->findAll("movies"); | |
} | |
} | |
class MovieLister | |
{ | |
private $movieFinder; | |
public function __construct(MovieFinder $movieFinder) { | |
$this->movieFinder = $movieFinder; | |
} | |
public function list() | |
{ | |
return $this->movieFinder->findAll(); | |
} | |
} | |
$container = new Container(); | |
$container->set("db", new SomeDatabaseMapper()); | |
$container->set("movie_finder", new MovieFinder($container->get("db"))); | |
$container->set("movie_lister", new MovieLister($container->get("movie_finder"))); | |
$movieList = $container->get("movie_lister")->list(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment