您的位置:首页 > 大数据 > 人工智能

Slim 框架学习,第五天 _Container(二)

2017-12-05 22:46 141 查看

今天主要分析一下container类的详细内容

首先是一些默认配置,看下代码

private $defaultSettings = [
'httpVersion' => '1.1',
'responseChunkSize' => 4096,
'outputBuffering' => 'append',
'determineRouteBeforeAppMiddleware' => false,
'displayErrorDetails' => false,
'addContentLengthHeader' => true,
'routerCacheFile' => false,
];


一看就懂,没什么可说的。

详细看下构造函数

public function __construct(array $values = [])
{
parent::__construct($values);

$userSettings = isset($values['settings']) ? $values['settings'] : [];
$this->registerDefaultServices($userSettings);
}


首先看下$values 的内容

请求地址:http://localhost:8080/hello/world

响应内容: array(0) {

}

在看下 parent::__construct($values);中的代码

Slim/vendor/pimple/pimple/src/Pimple/Container.php

public function __construct(array $values = array())
{
//SplObjectStorage是SPL标准库中的数据结构对象容器,用来存储一组对象,特别是当你需要唯一标识对象的时候
//PHP SPL SplObjectStorage类实现了Countable,Iterator,Serializable,ArrayAccess四个接口。可实现统计、迭代、序列化、数组式访问等功能。

$this->factories = new \SplObjectStorage();
$this->protected = new \SplObjectStorage();

foreach ($values as $key => $value) {
$this->offsetSet($key, $value);
}
}


这里有两个点需要注意下:

- SplObjectStorage是一个对象容器,用来存储对象的

- $this->offsetSet 该方法实现了ArrayAccess 接口中的方法

##### 看下$this->offsetSet的方法

public function offsetSet($id, $value)
{
if (isset($this->frozen[$id])) {
throw new FrozenServiceException($id);
}

$this->values[$id] = $value;
$this->keys[$id] = true;
}


都是一些很基础的内容,就是把对象,保存在了$this->values 中。

结束语:今天就先到这里,明天继续
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Slim