- 论坛徽章:
- 0
|
本帖最后由 satrun7 于 2016-12-13 17:39 编辑
- class IoC
- {
- protected static $registry = [];
-
- public static function bind($name, Callable $resolver)
- {
- static::$registry[$name] = $resolver;
- }
-
- public static function make($name)
- {
- if (isset(static::$registry[$name])) {
- $resolver = static::$registry[$name];
- return $resolver();
- }
- throw new Exception('Alias does not exist in the IoC registry.');
- }
- }
-
- IoC::bind('bim', function () {
- return new Bim();
- });
- IoC::bind('bar', function () {
- return new Bar(IoC::make('bim'));
- });
- IoC::bind('foo', function () {
- return new Foo(IoC::make('bar'));
- });
-
- $foo = IoC::make('foo');
- $foo->doSomething(); // Bim::doSomething|Bar::doSomething|Foo::doSomething
复制代码
来源 https://github.com/laracasts/simple-di-container/blob/master/IoC.php |
|