interface SuperModuleInterface { /** * 超能力激活方法 * * 任何一个超能力都得有该方法,并拥有一个参数 *@param array $target 针对目标,可以是一个或多个,自己或他人 */ public function activate(array $target); } class XPower implements SuperModuleInterface { public function activate(array $target) { echo 'xpower'; } } /** * 终极炸弹 (就这么俗) */ class UltraBomb implements SuperModuleInterface { public function activate(array $target) { echo 'ultrabomb'; } } class Superman { public $module; public function __construct(SuperModuleInterface $module) { $this->module = $module; } } 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) { // $this->binds['superman'] = function($container, $moduleName){ return new Superman($container->make($moduleName))}; return new Superman($container->make($moduleName)); }); //向该 超级工厂添加超能力模组的生产脚本 $container->bind('xpower', function($container) { //$this->binds['xpower'] = function($container){return new XPower;}; return new XPower; }); 同上 $container->bind('ultrabomb', function($container) { //$this->binds['ultrabomb'] = function($container){return new UltraBomb}; return new UltraBomb; }); // ****************** 华丽丽的分割线 ********************** // 开始启动生产 //相当于调用 $this->binds['superman']($container->make($moduleName)) $superman_1 = $container->make('superman', array('xpower')); $superman_2 = $container->make('superman', ['ultrabomb']); print_r($superman_1); //print_r($superman_2); $superman_1->module->activate([]);
php 简单ioc容器
最新推荐文章于 2021-07-08 12:38:27 发布