Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcelsud/6982166 to your computer and use it in GitHub Desktop.
Save marcelsud/6982166 to your computer and use it in GitHub Desktop.
<?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