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框架中三个绑定的源码分析。

如果您觉得我的文章有用,请随意打赏。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值