Thinkphp6-make() 、依赖注入 、 容器
2022-07-28 14:14:58
117
{{single.collect_count}}

容器

ThinkPHP使用容器来更方便的管理类依赖及运行依赖注入,新版的容器支持PSR-11规范。

make()

创建类的实例 已经存在则直接获取

  • 已经创建实例且不需要刷新 时直接返回
  • 函数需 先bind
  • 创建后保存实例到容器
  • 依赖注入
    在这里插入图片描述
  1. 获取别名
  2. 如果有实例直接获取 app->instances 中
  3. 如果在 app->bind 中 , invokeFunction 闭包 [使用 ReflectionFunction ]
  4. 类 invokeClass [使用 ReflectionClass]

依赖注入

依赖注入其实本质上是指对类的依赖通过构造器完成自动注入,例如在控制器架构方法和操作方法中一旦对参数进行对象类型约束则会自动触发依赖注入,由于访问控制器的参数都来自于URL请求,普通变量就是通过参数绑定自动获取,对象变量则是通过依赖注入生成。

Container.php / bindParams()
在这里插入图片描述
Container.php / getObjectParam( )
使用 make() 返回参数对象的实例
在这里插入图片描述

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}