Created
October 14, 2013 21:03
-
-
Save marcelsud/6982166 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 Container { | |
private $services = array(); | |
public function set($name, $instance) | |
{ | |
$this->services[$name] = $instance; | |
} | |
public function get($name) | |
{ | |
return $this->services[$name]; | |
} | |
} | |
interface MapperInterface | |
{ | |
public function findAll($table); | |
} | |
interface FinderInterface | |
{ | |
public function findAll(); | |
} | |
interface ListerInterface | |
{ | |
public function list(); | |
} | |
class SomeDatabaseMapper implements MapperInterface | |
{ | |
public function findAll($table) | |
{ | |
//Find all entries in some way | |
} | |
} | |
class MovieFinder implements FinderInterface | |
{ | |
private $db; | |
public function __construct(MapperInterface $db) { | |
$this->db = $db; | |
} | |
public function findAll() | |
{ | |
return $this->db->findAll("movies"); | |
} | |
} | |
class MovieLister implements ListerInterface | |
{ | |
private $movieFinder; | |
public function __construct(FinderInterface $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