php注册树模式,4. 创建容器 注册树模式

## 安装[PSR-11 容器接口]

`composer require psr/container`

![](https://img.kancloud.cn/a9/58/a958c45e8c7f6af10efe3fe8372c19f2_686x218.png)

安装完成可以看到 `vendor` 有 `psr/container`。

## 编辑app.php

```

define('FRAME_BASE_PATH', __DIR__); // 框架目录

define('FRAME_START_TIME', microtime(true)); // 开始时间

define('FRAME_START_MEMORY',memory_get_usage()); // 开始内存

class App implements Psr\Container\ContainerInterface {

public $binding = []; // 绑定关系

private static $instance; // 这个类的实例

protected $instances = []; // 所有实例的存放

private function __construct()

{

self::$instance = $this; // App类的实例

$this->register(); // 注册绑定

$this->boot(); // 服务注册了 才能启动

}

public function get($abstract)

{

if( isset($this->instances[$abstract])) // 此服务已经实例化过了

return $this->instances[$abstract];

$instance = $this->binding[$abstract]['concrete']($this); // 因为服务是闭包 加()就可以执行了

if( $this->binding[$abstract]['is_singleton']) // 设置为单例

$this->instances[$abstract] = $instance;

return $instance;

}

// 是否有此服务

public function has($id)

{

}

// 当前的App实例 单例

public static function getContainer()

{

return self::$instance ?? self::$instance = new self();

}

/**

*@param string $abstract 就是key

*@param void|string $concrete 就是value

*@param boolean $is_singleton 这个服务要不要变成单例

*/

public function bind($abstract, $concrete,$is_singleton = false)

{

if(! $concrete instanceof \Closure) // 如果具体实现不是闭包 那就生成闭包

$concrete = function ($app) use ($concrete) {

return $app->build($concrete);

};

$this->binding[$abstract] = compact('concrete','is_singleton'); // 存到$binding大数组里面

}

protected function getDependencies($paramters) {

$dependencies = []; // 当前类的所有依赖

foreach ($paramters as $paramter)

if( $paramter->getClass())

$dependencies[] = $this->get($paramter->getClass()->name);

return $dependencies;

}

// 解析依赖

public function build($concrete) {

$reflector = new ReflectionClass($concrete); // 反射

$constructor = $reflector->getConstructor(); // 获取构造函数

if( is_null($constructor))

return $reflector->newInstance(); // 没有构造函数? 那就是没有依赖 直接返回实例

$dependencies = $constructor->getParameters(); // 获取构造函数的参数

$instances = $this->getDependencies($dependencies); // 当前类的所有实例化的依赖

return $reflector->newInstanceArgs($instances); // 跟new 类($instances); 一样了

}

protected function register()

{

}

protected function boot()

{

}

}

```

## 编辑index.php

```

require __DIR__.'/../vendor/autoload.php';

require_once __DIR__.'/../app.php';

App::getContainer()->bind('str',function (){

return 'hello str';

});

echo App::getContainer()->get('str');

```

![](https://img.kancloud.cn/0e/24/0e24ae3d97ab7c9ba992093eaa7b0112_352x40.png)

## 关于此容器

看这些文章就可以解释了:

1. [注册树模式](https://learnku.com/docs/study-php-design-patterns/1.0/registration-tree-mode/8074)

2. [# 如何实现Ioc容器和服务提供者是什么概念](https://learnku.com/docs/laravel-core-concept/5.5/Ioc%E5%AE%B9%E5%99%A8,%E6%9C%8D%E5%8A%A1%E6%8F%90%E4%BE%9B%E8%80%85/3019) (很推荐看这个 看懂这个 本教程的容器你也懂了)

容器就是一个 [注册树模式](https://learnku.com/docs/study-php-design-patterns/1.0/registration-tree-mode/8074)

`注册树` 就是一个大数组, 数组嘛 就是 `key` 和 `value` 对应。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值