Facades门面技术
<?php
//facades门面技术
require 'model.php';
require 'view.php';
//1,服务容器
class container
{
protected $instances = [];
public function bind($alias,\Closure $process)
{
$this->instances[$alias] = $process;
}
public function make($alias,$params=[])
{
return call_user_func_array($this->instances[$alias],[]);
}
}
$container=new container;
$container->bind('Model',function(){return new Model;});
$container->bind('View',function(){return new View;});
//2,facades门面技术
class facades
{
protected static $container=null;
protected static $data=[];
// 初始化方法: 从外部接收一个依赖对象: 服务容器的实例
public static function initialize(container $container)
{
static::$container = $container;
}
}
class M extends facades
{
public static function getData()
{
static::$data= static::$container->make('Model')->getData();
}
}
class V extends facades
{
public static function fetch()
{
return static::$data = static::$container->make('View')->fetch(static::$data);
}
}
//3,控制器
class controller
{
public function __construct(container $container)
{
facades::initialize($container);
}
public function index()
{
M::getData();
return V::fetch();
}
}
$controller=new controller($container);
echo $controller->index();
总结:运行原理应该是这样的;从底层开始(控制器)-上移->Facades门面-上移->服务容器。往上接管!代码看的懵!