Skip to content

Instantly share code, notes, and snippets.

@marcelsud
Last active December 25, 2015 12:49
Show Gist options
  • Save marcelsud/6978750 to your computer and use it in GitHub Desktop.
Save marcelsud/6978750 to your computer and use it in GitHub Desktop.
<?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();
@marcelsud
Copy link
Author

Three words about it...

Avoid... avoid... avoid!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment