溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

PHP數組式訪問-ArrayAccess的案例分析

發布時間:2020-11-02 14:42:18 來源:億速云 閱讀:186 作者:小新 欄目:編程語言

PHP數組式訪問-ArrayAccess的案例分析?這個問題可能是我們日常學習或工作經常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家帶來的參考內容,讓我們一起來看看吧!

  以前對ArrayAccess不是很熟悉,現在整理下下有關ArrayAccess相關的知識,ArrayAccess接口就是提供像訪問數組一樣訪問對象的能力的接口。

接口內容如下:

ArrayAccess {
    //檢查一個偏移位置是否存在 
    abstract public boolean offsetExists ( mixed $offset ); 
    //獲取一個偏移位置的值 
    abstract public mixed offsetGet ( mixed $offset ); 
    //設置一個偏移位置的值 
    abstract public void offsetSet ( mixed $offset , mixed $value ); 
    //復位一個偏移位置的值 
    abstract public void offsetUnset ( mixed $offset ); 
}

項目中使用,獲取網站配置:

<?php
namespace lib;
use mpf\core\Di;
class config implements \ArrayAccess{

//定義存儲數據的數組
   protected $configs;
   public function __construct($configs){
         $this->configs = $configs;
         $configs = \lib\model\Home::getWebConfig();
         foreach( $configs as $config ){
               if( !isset($this->configs[$config['sc_key']]) ){
                   $this->configs[$config['sc_key']] = $config['sc_content'];
               }
         }
   }
   public function get($key){
         if( isset($this->configs[$key]) ){
               return $this->configs[$key];
         }elseif( $key == 'caipiao'){
               $this->configs['caipiao'] = \lib\model\Home::getLcs();  
               return $this->configs[$key];
         }elseif( $key == 'user_money' ){
               if( isset($_SESSION['uid']) ){
                 if( $_SESSION['utype'] == 5 ){
                       $sql = 'select money from inner_user where uid=?';
                 }else{
                       $sql = 'select money from user where uid=?';
                 }
                   $this->configs['user_money'] = \mpf\core\Di::$Di->db->prepare_query($sql,[getUid()])->fetch(\PDO::FETCH_COLUMN);
                   return $this->configs['user_money'];
             }
       }
   }
   public function offsetExists($index){
         return isset($this->configs[$index]);
   }
   public function offsetGet($index){
         return $this->configs[$index];
   }
   public function offsetSet($index,$val){
         $this->configs[$index] = $val;
   }
   public function offsetUnset($index){
         unset($this->configs[$index]);
   }
}

這樣可以使用config對象來直接訪問配置信息內容。

配置程序:

我們可以通過ArrayAccess利用配置文件來控制程序。

1. 在項目更目錄下創建一個config目錄
2. 在config目錄下創建相應的配置文件,比如app.php 和 database.php。文件程序如下

app.php

<?phpreturn [   
 'name' => 'app name',   
  'version' => 'v1.0.0'
];

database.php

<?php

return [
    'mysql' => [
        'host' => 'localhost',
        'user' => 'root',
        'password' => '12345678'
    ]
];

3. Config.php實現ArrayAccess

<?php

namespace Config;

class Config implements \ArrayAccess
{
    private $config = [];

    private static $instance;

    private $path;

    private function __construct()
    {
        $this->path = __DIR__."/config/";
    }

    public static function instance()
    {
        if (!(self::$instance instanceof Config)) {
            self::$instance = new Config();
        }
        return self::$instance;
    }
    
    public function offsetExists($offset)
    {
        return isset($this->config[$offset]);
    }
    
    public function offsetGet($offset)
    {
        if (empty($this->config[$offset])) {
            $this->config[$offset] = require $this->path.$offset.".php";
        }
        return $this->config[$offset];
    }

    public function offsetSet($offset, $value)
    {
        throw new \Exception('不提供設置配置');
    }

    public function offsetUnset($offset)
    {
        throw new \Exception('不提供刪除配置');
    }
}

$config = Config::instance();

//獲取app.php 文件的 name
echo $config['app']['name'].PHP_EOL; //app name

//獲取database.php文件mysql的user配置
echo $config['database']['mysql']['user'].PHP_EOL; // root

感謝各位的閱讀!看完上述內容,你們對PHP數組式訪問-ArrayAccess的案例分析大概了解了嗎?希望文章內容對大家有所幫助。如果想了解更多相關文章內容,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女