ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## **ArrayAccess** >[info] 将对象当做数组一样操作 **最简单的一个例子:** ``` class Test implements \ArrayAccess{ private $configs; public function offsetExists($key) { return isset($this->configs[$key]); } public function offsetSet($key, $value) { $this->configs[$key] = $value; } public function offsetGet($key) { return $this->configs[$key]; } public function offsetUnset($key) { unset($this->configs[$key]); } } $obj = new Test(); $data='data'; //自动调用offsetSet方法 $obj['data'] = $data; //自动调用offsetExists if(isset($obj['data'])){ echo 'has setting!'; } //自动调用offsetGet var_dump($obj['data']); //自动调用offsetUnset unset($obj['data']); var_dump($obj['data']);//自动调用offsetExists //输出: //has setting! //data //null ``` **自动加载缓存文件类** ``` class AutoCache implements \ArrayAccess { protected $path; protected $cachedir = array(); /** * 析构函数初始化缓存文件路径 */ function __construct($path) { $this->path = $path; } /** * * 获取一个偏移位置的值 * 例子:$cache=new Config($dirpath); * 调用此方法的写法:$cache[$key] */ function offsetGet($key) { if (empty($this->cachedir[$key])) { $file_path = $this->path.'/'.$key.'.php'; $cachefile = require $file_path; $this->cachedir[$key] = $cachefile; } return $this->cachedir[$key]; } /** * 设置一个偏移位置的值 * 例子:$cache=new Config($dirpath); * 调用此方法的写法:$cache[$key]='modules'; */ function offsetSet($key, $value) { if (is_null($key)) { throw new \Exception("请为你的缓存数据的文件起个文件名吧"); } //文件还未被缓存过 if (empty($this->cachedir[$key])) { $file= $this->path.'/'.$key.'.php'; $array=object_array($value); $aaa=file_put_contents($file,"<?php \r\n \$arr=".var_export($array, true)."; \r\n return \$arr;"); //加var_export的原因是file_put_contents不支持多维数组的数据 if($aaa){ $cachefile = require $file; //var_dump($cachefile); $this->cachedir[$key] = $cachefile; } } return $this->cachedir[$key]; } /** * 检查一个偏移位置是否存在 * 例子:$cache=new Config($dirpath); * 调用此方法的写法:isset($cache[$key]); */ function offsetExists($key) { if (empty($this->cachedir[$key])) { echo'meiyou'; //加载文件后设置偏移位置,然后在判断 $file_path = $this->path.'/'.$key.'.php'; var_dump($file_path); if(file_exists($file_path)){ $cachefile = require $file_path; $this->cachedir[$key] = $cachefile; } } echo '调用了判断存在的方法offsetExists <br>'; var_dump(isset($this->cachedir[$key])); echo'<br />'; return isset($this->cachedir[$key]); } /** * 复位一个偏移位置的值 * 例子:$cache=new Config($dirpath); * 调用此方法的写法:isset($cache[$key]); */ function offsetUnset($key) { echo '调用了删除的方法offsetUnset <br>'; $file_path = $this->path.'/'.$key.'.php'; if(unlink($file_path)) { unset($this->cachedir[$key]); } } } ``` **可以用于读取网站的配置文件:** database.php内容: ~~~ return [     'mysql' => [         'host' => 'localhost',         'user' => 'root',         'password' => '12345678'     ] ]; ~~~ moudles.php的内容: ~~~ $config = array(     'home' => array(          'decorator' => array(                'App\Decorator\Login',                'App\Decorator\Template',                'App\Decorator\Json',          ),     ),     'default' => 'hello world', ); return $config; ~~~ ~~~ namespace Config; class Config implements \ArrayAccess {     private $config = [];     private static $instance;     private $path;     //配置文件路径并存放进$path变量     private function __construct()     {         $this->path = __DIR__."/configs/";     }     public static function instance()     {         if (!(self::$instance instanceof Config)) {             self::$instance = new Config();         }         return self::$instance;     }         public function offsetExists($key)     {         return isset($this->config[$key]);     }         public function offsetGet($key)     {         if (empty($this->config[$key])) {           //键对应的值为空则设置值             $this->config[$key] = require $this->path.$key.".php";         }         return $this->config[$key];     }     public function offsetSet($key, $value)     {         throw new \Exception('不提供设置配置');     }     public function offsetUnset($key)     {         throw new \Exception('不提供删除配置');     } } ~~~ 使用: ~~~ $config = Config::instance(); //获取database.php文件mysql的user配置 echo $config['database']['mysql']['user'].PHP_EOL; // root //获取database.php文件mysql的user配置 echo $config['moudles']['default'].PHP_EOL; // PHP_EOL:换行符 ~~~