15.自动加载配置
2016-03-07 22:09
330 查看
自动加载配置
1. 在PHP 中使用 ArrayAccess 实现配置文件的加载
Config.php
controller.php
index.php
1. 在PHP 中使用 ArrayAccess 实现配置文件的加载
Config.php
<?php namespace IMooc; class Config implements \ArrayAccess { //$path,配置文件所在的目录 protected $path; //$configs,用来保存已经加载过的配置 protected $configs = array(); function __construct($path) { $this->path = $path; } 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]); } }
controller.php
<?php $config = array( 'home' => array( 'decorator' => array( 'IMooc\Decorator\Template', ), ), 'default' => 'hello ,world', ); return $config;
index.php
$config = new \IMooc\Config(__DIR__ . '/configs'); var_dump($config['controller']);
相关文章推荐
- urllib
- Java ExecutorService, FutureTaskd
- 栈
- cf#345-A - Joysticks-模拟题
- 2016-03-07
- cocos2dx的jni使用
- php 数字格式化输出
- C++封装学习(二)
- 必备收款工具
- 个人书签(不断更新)
- Servlet入门案例
- 用UI简单的控件搭载一个简单的登录页面
- 蓝桥杯_基础练习《时间转换---30》
- 213. House Robber II LeetCode
- JS初步学习
- 归并排序(Merge Sort)
- 【总结】C++静态成员变量的特性总结及测试用例
- struts2 表单验证
- 数值分析PTA第一题
- android完全退出程序