扫码关注官方订阅号
为什么在 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 的来源~
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
还是早晨脑子清醒,把 Container 类看了一遍找到 $this->app 咋来的了。
Container 类实现了 ArrayAccess 接口,关于 ArrayAccess 接口的介绍可以参考官方文档
现在很多moden php 框架都使用了这个东西。
当访问 $this->app 的时候默认调用的 Container 类中的 offsetGet 方法,然后 offsetGet 调用了 Container 类的 make 方法这个方法是 根据传入的类型从Container 中获取类型实例的方法。看到 make 方法的代码
瞬间就明白了 $this->app 的来源~