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 协议》,转载必须注明作者和本文链接
推荐文章: