<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                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:換行符 ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看