您的位置:首页 > 编程语言 > PHP开发

关于laravel 自动加载和依赖注入、服务容器、服务提供者的理解--1

2017-09-23 19:18 627 查看
参考实例
http://laravelacademy.org/post/769.html

Laravel中 使用composer下载包时  会在vendor下有个autoload.php文件 
这个autoload.php文件解决的是自动加载的问题,通俗点讲:就是通过 _autoload 或者 spl_autoload_register 来解决频繁使用
require_once  的问题 
_autoload是在new Class实例化的时候自动调用的函数 

依赖注入:通俗点讲:就是在一个class a中 需要new 另外一个class B 那么在实例化a的时候就依赖B  
它解决的是何时去new Class 实例化的问题  

class a{
    Private $b;
    Public function __construct(){
        $b = new B(); 
    }
}

工厂模式 : 通俗点说,就是在一个类中 将所有的 new Class 实例化的工作去交给一个工厂类去办,工厂类直接返回实例

class SuperModuleFactory
{
    public function makeModule($moduleName, $options)
    {
        switch ($moduleName) {
            case 'Fight':
                return new Fight($options[0], $options[1]);
            case 'Force':
                return new Force($options[0]);
            case 'Shot':
                return new Shot($options[0], $options[1], $options[2]);
        }
    }
}

class Superman
{
    protected $power;

    public function __construct()
    {
        // 初始化工厂
        $factory = new SuperModuleFactory;

        // 通过工厂提供的方法制造需要的模块
        $this->power = $factory->makeModule('Fight', [9, 100]);
        // $this->power = $factory->makeModule('Force', [45]);
        // $this->power = $factory->makeModule('Shot', [99, 50, 2]);
        /*
        $this->power = array(
            $factory->makeModule('Force', [45]),
            $factory->makeModule('Shot', [99, 50, 2])
        );
        */
    }
}

IoC模式:通俗点说,就是不去new Class啦,在一个类中如果需要另外一个class,那么就直接将这个class的实例作为参数传递进去

class Superman
{
    protected $module;
    public function __construct(SuperModuleInterface $module)
    {
        $this->module = $module;
    }
}

// 超能力模组
$superModule = new XPower;
// 初始化一个超人,并注入一个超能力模组依赖
$superMan = new Superman($superModule);

public function store(Request $request)  //这里$request 就是Request类的实例 作为参数传递进来  直接使用$request来获取get或者post过来的数据
{
    $name => $request->name,
    $email=> $request->email,

}

IoC容器:通俗点说,就是升级版的“超级工厂” 也就是将IoC和工厂模式结合起来,通过向“超级工厂”添加闭包函数来将IoC模式中的
new XPower
new Superman通过工厂模式生产出来

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->make($moduleName)就是IoC模式中 向Superman类注入的实例
});

// 向该 超级工厂添加超能力模组的生产脚本
$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_3 = $container->make('superman', 'xpower');
// ...随意添加

laravel中的服务容器 其实就是一个IoC容器  服务器提供者其实就是将bind,make这些同一放到一个serviceProvider类中
关于服务容器,服务提供者的进一步理解请参考 <<laravel关键技术解析第150页>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐