laravel 框架 三种绑定 bind、singleton、instance 源码分析

栏目: PHP · 发布时间: 6年前

内容简介:laravel 框架 三种绑定 bind、singleton、instance 源码分析

laravel 框架采用IoC模式即(inversion  of  Controller)控制反转进行类的操作。将要使用的类提前绑定到容器中。今天我们讲解的不是IoC,主要通过 laravel 源码分析三种绑定的区别,有利于大家对laravel框架有更进一步的了解。

一、instance 绑定

首先我们来看一下instance方法的代码

public function instance($abstract, $instance)
{

    //移除已经存在的抽象的别名
    $this->removeAbstractAlias($abstract);
   //检查是否已经存在该别名的绑定的对象
    $isBound=$this->bound($abstract);
    unset($this->aliases[$abstract]);

    // We'll check to determine if this type has been bound before, and if it has
    // we will fire the rebound callbacks registered with the container and it
    // can be updated with consuming classes that have gotten resolved here.
    $this->instances[$abstract] = $instance;

    // 判断是否已经绑定
    if ($isBound) {
        $this->rebound($abstract);
    }
}

可以看到在instance方法中,首先移除已经存在的相同的别名,然后将对象存入$this->instance 数组中。然后完成了绑定。

二、bind 绑定

同样的,我们先贴出代码

public function bind($abstract, $concrete = null, $shared = false)
{
    // If no concrete type was given, we will simply set the concrete type to the
    // abstract type. After that, the concrete type to be registered as shared
    // without being forced to state their classes in both of the parameters.

    //移除旧的实例
    $this->dropStaleInstances($abstract);
    if (is_null($concrete)) {
        $concrete = $abstract;
    }
    // If the factory is not a Closure, it means it is just a class name which is
    // bound into this container to the abstract type and we will just wrap it
    // up inside its own Closure to give us more convenience when extending.
    if (! $concrete instanceof Closure) {
        $concrete = $this->getClosure($abstract, $concrete);
    }
    // 创建一个包含变量与其值的数组。
    //对每个参数,compact() 在当前的符号表中查找该变量名并将它添加到输出的数组中,变量名成为键名而变量的内容成为该键的值。简单说,它做的事和 extract() 正好相反。返回将所有变量添加进去后的数组。
    $this->bindings[$abstract] = compact('concrete', 'shared');

    // If the abstract type was already resolved in this container we'll fire the
    // rebound listener so that any objects which have already gotten resolved
    // can have their copy of the object updated via the listener callbacks.
    //检查是否解析过 或则 已经存在所要绑定对象对应的实例
    if ($this->resolved($abstract)) {
        $this->rebound($abstract);
    }
}

使用bind 的形式如下:

$this->app->bind('HelpSpot\API', function ($app) {
    return new HelpSpot\API($app->make('HttpClient'));
});

在bind 方法中 首先移除旧的实例,绑定的对象实例一般是放在闭包中,如果第二个参数不是闭包,是类名,会通过getClosure函数将类名封装进闭包中,然后在闭包中通过容器的make或build函数解析该类。绑定会将相应的闭包以及是否share 放进$this->bindings数组中。在解析的时候调用。

singleton 绑定

/**
 * Register a shared binding in the container.
 *  绑定到容器的对象只会被解析一次,之后的调用都返回相同的实例:
 * @param  string|array  $abstract
 * @param  \Closure|string|null  $concrete
 * @return void
 */
public function singleton($abstract, $concrete = null)
{
    $this->bind($abstract, $concrete, true);
}

可以知道singleton 只是bind的一个调用。

这就是对laravel框架中三个绑定的源码分析。


以上所述就是小编给大家介绍的《laravel 框架 三种绑定 bind、singleton、instance 源码分析》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

云计算安全与隐私

云计算安全与隐私

Tim Mather、Subra Kumaraswamy、Shahed Latif / 刘戈舟、杨泽明、刘宝旭 / 机械工业出版社华章公司 / 2011-6 / 65.00元

《云计算安全与隐私》可以使你明白当把数据交付给云计算时你所面临的风险,以及为了保障虚拟基础设施和网络应用程序的安全可以采取的行动。本书是由信息安全界知名专家所著,作者在书中给出许多中肯的忠告和建议。本书的读者对象包括:IT职员、信息安全和隐私方面的从业人士、业务经理、服务提供商,以及投资机构等。阅读本书你会了解直到现在还严重匮乏的云计算安全方面的详尽信息。 《云计算安全与隐私》主要内容包括:......一起来看看 《云计算安全与隐私》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

html转js在线工具
html转js在线工具

html转js在线工具