php设计模式---自动加载配置
2016-09-21 19:53
423 查看
<?php
//自动加载配置
class Config implements ArrayAccess
{
protected $path;
protected $configs = array();//用来保存已经加载过的配置
public function __construct($path){
//path为配置文件所在目录
$this->path = $path;
}
public function offsetGet($key){
if(!isset($this->configs[$key])){
//先生成一个绝对文件的路径
$file_path = $this->path . '/' . $key . ".php";
//判断该文件是否存在
if(!file_exists($file_path)){//文件不存在,返回-1
return -1;
}
//将配置信息保存到数组中
$this->configs[$key] = require("$file_path");
}
return $this->configs[$key];
}
public function offsetSet($key, $value){
//设置数组的key
$this->configs[$key] = $value;
}
public function offsetExists($key){
//检测数组的key是否存在
return isset($this->configs[$key]);//如果存在则返回true,否则返回false
}
public function offsetUnset($key){
//删除数组的key
if(isset($this->configs[$key])){
//存在数组的key值,则删除其值,返回true值
unset($this->configs[$key]);
return true;
}else{
//不存在数组的key值,返回false值
return false;
}
}
}
//自动加载配置
class Config implements ArrayAccess
{
protected $path;
protected $configs = array();//用来保存已经加载过的配置
public function __construct($path){
//path为配置文件所在目录
$this->path = $path;
}
public function offsetGet($key){
if(!isset($this->configs[$key])){
//先生成一个绝对文件的路径
$file_path = $this->path . '/' . $key . ".php";
//判断该文件是否存在
if(!file_exists($file_path)){//文件不存在,返回-1
return -1;
}
//将配置信息保存到数组中
$this->configs[$key] = require("$file_path");
}
return $this->configs[$key];
}
public function offsetSet($key, $value){
//设置数组的key
$this->configs[$key] = $value;
}
public function offsetExists($key){
//检测数组的key是否存在
return isset($this->configs[$key]);//如果存在则返回true,否则返回false
}
public function offsetUnset($key){
//删除数组的key
if(isset($this->configs[$key])){
//存在数组的key值,则删除其值,返回true值
unset($this->configs[$key]);
return true;
}else{
//不存在数组的key值,返回false值
return false;
}
}
}
相关文章推荐
- 慕课网----大话PHP设计模式 一(命名空间,类的自动加载,PSR-0)
- 慕课网----大话PHP设计模式 六(面向对象编程的基本原则,自动加载config)
- php设计模式 一 (命名空间 类自动载入 PSR标准)
- php composer 在laravel中自动加载autoload_class.php中的配置
- php设计模式--命名空间与自动载入
- 【设计模式】C++单例模式的几种写法——Java自动加载内部类对象,C++怎么破?
- 【小结】SPL 自动加载(from《PHP高级程序设计 模式、框架与测试》)
- tp3.2不能自动加载公共配置文件config.php
- 设计模式之PHP项目应用——策略模式设计自动驾驶系统
- PHP 设计模式 笔记与总结(1)命名空间 与 类的自动载入
- PHP Strom 配置less 并设置编译后在远程开发模式下自动上传css文件
- tp3.2不能自动加载公共配置文件config.php
- (十五)Core Java Eclipse的使用(快捷配置,模板代码.静态导入,自动拆装箱,享元设计模式) (112)
- Phalcon 的 bootstrap.php 自动加载完成;非常人性化的设计
- CodeIgniter配置之autoload.php自动加载用法分析
- CodeIgniter配置之autoload.php自动加载用法分析
- PHP Strom 配置less 并设置编译后在远程开发模式下自动上传css文件
- 设计模式(三)——自动加载
- Android在debug和release模式自动加载不同的配置文件
- [介绍]PHP设计模式:DAO(数据访问对象模式)