企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 服务容器 框架使用 Contrainer 组件完成对象的管理。 其他产品也可以使用该组件,请登录 [GITHUB](https://github.com/houdunwang/container) 查看源代码与说明文档。 ## 使用 我们先定义一个测试类 ``` class Test{ public function show(){return 'success';} } ``` #### 注入单例对象 ``` App::instance('app', new Test()); App::make('app')->show(); ``` #### 使用single注入单例 ``` App::single('app',function () { return new Test(); }); App::make('app')->show(); ``` #### 使用bind注入到容器 ``` App::bind('app',function () { return new Test(); }); App::make('app')->show(); ``` #### 调用类方法 调用类方法时组件会分析方法参数实现依赖注入 ``` $res = App::callMethod(Test::class, 'show'); ``` #### 调用函数 调用函数时组件会分析函数的参数实现依赖注入 ``` $res = App::callFunction(function (Test $app) { return $app->show(); }); ```