-
-
Save J7mbo/5524740 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
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