您的位置:首页 > 编程语言 > PHP开发

php的内置接口 arrayaccess

2017-07-03 00:22 267 查看
//ArrayAccess 是php内置的接口 可以把配置文件直接读出来
class Config implements ArrayAccess{
protected $path;
protected $config = [];

//'offsetGet', 'offsetUnset', 'offsetExists', 'offsetSet'
public function __construct($path){
$this->path = $path;
}
//获取数组的key
public function offsetGet($key){
if(empty($this->config[$key])){
echo $this->path;
$file_path = $this->path.'/'.$key.'.php';
$config = require $file_path;
$this->config[$key] = $config;
}
return $this->config[$key];
}
public function offsetSet($key,$value){

}
public function offsetUnset($key){

}
public function offsetExists($key){
return isset($this->config[$key]);
}
}
$config = new Config(__DIR__.'/config');
var_dump($config['config']);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: