输入banner图图片脚本导航/分类

laravel5中$this->app是哪里赋值的

为什么在 instances 方法中对属性 instances 数组赋值后。
$this->app 便有了值。

回复内容:

为什么在 instances 方法中对属性 instances 数组赋值后。
$this->app 便有了值。

还是早晨脑子清醒,把 Container 类看了一遍找到 $this->app 咋来的了。
Container 类实现了 ArrayAccess 接口,关于 ArrayAccess 接口的介绍可以参考官方文档

现在很多moden php 框架都使用了这个东西。

当访问 $this->app 的时候默认调用的 Container 类中的 offsetGet 方法,然后 offsetGet 调用了 Container 类的 make 方法这个方法是 根据传入的类型从Container 中获取类型实例的方法。看到 make 方法的代码

if (isset($this->instances[$abstract])) {
            return $this->instances[$abstract];
        }

瞬间就明白了 $this->app 的来源~