mvc与服务容器类
<?php
namespace mvc_demo;
// 控制器依赖注入点改到构造方法, 实现对外部依赖对象的共享
require 'UserM.php';
require 'UserV.php';
class Container1
{
// 对象容器
protected $instances = [];
// 绑定: 向对象容器中添加一个类实例
public function bind($alias, \Closure $process)
{
$this->instances[$alias] = $process;
}
// 取出: 从容器中取出一个类实例 (new)
public function make($alias,$params = [])
{
return call_user_func_array($this->instances[$alias],[]);
}
}
$container = new Container1();
$container->bind('model',function (){return new UserM;});
$container->bind('view',function (){return new UserV;});
class Contraller4
{
public function index(Container1 $container)
{
//获取数据
$data = $container->make('model')->getData();
//渲染模板
return $container->make('view')->fatch($data);
}
public function __destruct(){
}
}
//实例化控制器类
$contraller = new Contraller4();
echo $contraller->index($container);
unset($contraller);