12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- namespace Core;
- /**
- * 配置文件的加载 基于ArrayAccess
- * 可以让对象像数组一样进行访问
- * Class Config
- * @package IMooc
- */
- class Config implements \ArrayAccess
- {
- protected $path;
- protected $configs = array();
- function __construct($path)
- {
- $this->path = $path;
- }
- /**
- * 获取配置
- * @param mixed $key
- * @return mixed
- */
- function offsetGet($key)
- {
- if (empty($this->configs[$key]))
- {
- $file_path = $this->path.'/'.$key.'.php';
- $config = require $file_path;
- $this->configs[$key] = $config;
- }
- return $this->configs[$key];
- }
- function offsetSet($key, $value)
- {
- throw new \Exception("cannot write config file.");
- }
- function offsetExists($key)
- {
- return isset($this->configs[$key]);
- }
- function offsetUnset($key)
- {
- unset($this->configs[$key]);
- }
- }
|