1 Star 0 Fork 0

hfu / container-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

一个简易的服务容器模式(LoC)的实现,本包将实现以下功能:

  • 对象实例化
  • 服务绑定
  • 依赖注入

快速入门

获取服务实例

$app = Bg\Container\Container::getInstance();

实例化一个对象

$app->make(\App\Example::class);

依赖注入

假如现在有一个类 \App\Example ,其依赖于类 \App\Inject ,代码如下:

namespace App;

class Example
{

    private $obj;
    
    public function __construct(Inject $obj)
    {
        $this->obj = $obj;
    }
    
    
    public function getValue()
    {
        return $this->obj->getValue();
    }
}



class Inject
{
    public function getValue()
    {
        return 'inject';
    }
}

类 \App\Example 的构造函数需要 \App\Inject 的实例参数,使用服务容器,我们不需要手工对 \App\Inject 实例化,服务容器将替我们完成这项工作:

$example = $app->make(\App\Example::class);
echo($example->getValue());

以上例子将输出: ==inject==

空文件

简介

暂无描述 展开 收起
PHP
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/haifu_xu/container-demo.git
git@gitee.com:haifu_xu/container-demo.git
haifu_xu
container-demo
container-demo
master

搜索帮助