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

clipboard.png

clipboard.png

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

阅读 4.7k
1 个回答

还是早晨脑子清醒,把 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 的来源~

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题