Skip to content

Instantly share code, notes, and snippets.

@J7mbo
Forked from Ocramius/BasicDicContainer.php
Created May 6, 2013 12:03
Show Gist options
  • Save J7mbo/5524740 to your computer and use it in GitHub Desktop.
Save J7mbo/5524740 to your computer and use it in GitHub Desktop.
<?php
class B {
protected $c;
public function __construct(C $c) {
$this->c = $c;
}
}
class C {}
class MyContainer
{
protected $instances = array();
public function __construct()
{
$this->instances = array(
'B' => function (MyContainer $container) {
return new B($container->get('C'));
},
'C' => function(MyContainer $container) {
return new C();
},
);
}
public function get($name)
{
$cb = $this->instances[$name];
if ($cb instanceof \Closure) {
$this->instances[$name] = $cb($this);
}
return $this->instances[$name];
}
}
$container = new MyContainer();
var_dump($container->get('B'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment