//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']);
php的内置接口 arrayaccess
最新推荐文章于 2021-03-23 18:20:50 发布