IoC(控制反转)的理解笔记

阅读laravel文档时看到服务容器时无法理解,于是从论坛找到一篇不错的文章-- Laravel 学习笔记 —— 神奇的服务容器,PHP基础不是很好,花了很长时间才理解。整理了一下自己的傻瓜式理解过程。

范例代码:

<?php
class Superman
{
    public $module;
    public function __construct(SuperModuleInterface $module)
    {
        $this->module = $module;
    }
}
interface SuperModuleInterface
{
    public function activate(array $target = []);
}
class XPower implements SuperModuleInterface
{
    public function activate(array $target = [])
    {
        echo "X-超能量";
    }
}
class UltraBomb implements SuperModuleInterface
{
    public function activate(array $target = [])
    {
        echo "终极炸弹";
    }
}
class Container
{
    protected $binds;
    protected $instances;
    public function bind($abstract, $concrete)
    {
        if ($concrete instanceof Closure) {
            $this->binds[$abstract] = $concrete;
        } else {
            $this->instances[$abstract] = $concrete;
        }
    }
    public function make($abstract, $parameters = [])
    {
        if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }
        array_unshift($parameters, $this);
        return call_user_func_array($this->binds[$abstract], $parameters);
    }
}

// 创建一个容器(后面称作超级工厂)
$container = new Container;
// 向该 超级工厂 添加 超人 的生产脚本
$container->bind('superman', function($container, $moduleName) {
    return new Superman($container->make($moduleName));
});
// 向该 超级工厂 添加 超能力模组 的生产脚本
$container->bind('xpower', function($container) {
    return new XPower;
});
 同上
$container->bind('ultrabomb', function($container) {
    return new UltraBomb;
});
// 开始启动生产
$superman_1 = $container->make('superman', ['xpower']);
$superman_2 = $container->make('superman', ['ultrabomb']);

$superman_1->module->activate();
$superman_2->module->activate();

理解过程:

$container = new Container;
$container->bind('superman', function($container, $moduleName) {
         return new Superman($container->make($moduleName));
});
/*对上面代码的理解
$this->binds[superman] = function($container, $moduleName) {
         return new Superman($container->make($moduleName));
};
*/
$container->bind('xpower', function($container) {
         return new XPower;
});
/*对上面代码的理解
$this->binds['xpower'] = function($container) {
         return new XPower;
};
*/
$superman_1 = $container->make('superman', ['xpower']);
/*对上面代码的理解
$abstract= 'superman';
$parameters= ['xpower'];

array_unshift($parameters, $this);
$parameters =[$container,'xpower'];

call_user_func_array($this->binds[$abstract], $parameters);
call_user_func_array($this->binds[$superman], $parameters);
call_user_func_array( $this->binds['superman']=function($container, $moduleName) {
         return new Superman($container->make($moduleName));
} ,[$container,'xpower']);
$this->binds['superman'] = function($container, 'xpower') {
         return new Superman($container->make('xpower'));
};
$this->binds['superman'] = function($container, 'xpower') {
         return new Superman(call_user_func_array($this->binds('xpower'), [ ]));
};
$this->binds['superman'] = function($container, 'xpower') {
         return new Superman(function([]) {
                return new XPower;
         });
};
$superman_1 =new Superman($this->module=new XPower;);
*/
$superman_1->module->activate();
/*
echo "X-超能量";
*/
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!