Blogger Information
Blog 43
fans 0
comment 0
visits 26791
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
依赖注入、容器、facede案例+2018年10月22日
Lee的博客
Original
603 people have browsed it

依赖注入案例

实例

<?php
require_once 'Model.php';
require_once 'View.php';

class Controller
{
    protected $View = null;
    protected $Model = null;
    public function __construct(Model $Model,View $View)
    {
        $this->view = $View;
        $this->model = $Model;
    }

    public function index(){
        $data = $this->model->getData();
        return $this->view->fetch($data);
    }
}

$View = new View();
$Model = new Model();
$controller = new Controller($Model,$View);
echo $controller->index();

运行实例 »

点击 "运行实例" 按钮查看在线实例



容器案例

实例

<?php
require_once 'Model.php';
require_once 'View.php';

class Container
{
    protected $instance = [];
    public function bind($lee,Closure $process){
        $this->instance[$lee] = $process;
    }

    public function make($lee,$params=[]){
        return call_user_func_array($this->instance[$lee],[]);
    }
}

$container = new Container();
$container->bind('model',function (){return new Model();});
$container->bind('view',function (){return new View();});

class Controller
{
    public function index(Container $container){
        $data = $container->make('model')->getData();
        return $container->make('view')->fetch($data);
    }
}

$controller = new Controller();
echo $controller->index($container);

运行实例 »

点击 "运行实例" 按钮查看在线实例



Facade 案例

实例

<?php
require_once 'Model.php';
require_once 'View.php';

class Container
{
    protected $instance = [];
    public function bind($lee,Closure $process){
        $this->instance[$lee] = $process;
    }

    public function make($lee,$params=[]){
        return call_user_func_array($this->instance[$lee],[]);
    }
}

$container = new Container();
$container->bind('model',function (){return new Model();});
$container->bind('view',function (){return new View();});

class Facade
{
    protected static $container = null;
    protected static $data = [];
    public static function initialize(Container $container){
        static::$container = $container;
    }
    public static function getData(){
        static::$data = static::$container->make('model')->getData();
    }
    public static function fetch(){
        return static::$container->make('view')->fetch(static::$data);
    }
}

class Student extends Facade
{

}

class Controller
{
    public function __construct(Container $container){
        Student::initialize($container);
    }

    public function index(){
        Student::getData();
        return Student::fetch();
    }
}
$controller = new Controller($container);
echo $controller->index();

运行实例 »

点击 "运行实例" 按钮查看在线实例


QQ截图20181024161811.bmp


总结,越做越复杂!


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post