PHP預定義介面:ArrayAccess

來源:互聯網
上載者:User

標籤:應用   rgs   ase   目錄   zhang   簡介   one   public   database   

簡介
  • 提供像訪問數組一樣訪問對象的能力的介面。
介面摘要
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 )}
目錄
  • ArrayAccess::offsetExists — 檢查一個位移位置是否存在
  • ArrayAccess::offsetGet — 擷取一個位移位置的值
  • ArrayAccess::offsetSet — 設定一個位移位置的值
  • ArrayAccess::offsetUnset — 複位一個位移位置的值

代碼示範

class Obj implements ArrayAccess{    private $container = [];    public function offsetExists($offset): bool    {        echo ‘調用‘ . __METHOD__ . ‘方法‘ . PHP_EOL;        echo print_r(func_get_args(), true);        return isset($this->container[$offset]);    }    public function offsetGet($offset)    {        echo ‘調用‘ . __METHOD__ . ‘方法‘ . PHP_EOL;        echo print_r(func_get_args(), true);        return $this->container[$offset] ?? null;    }    public function offsetSet($offset, $value)    {        echo ‘調用‘ . __METHOD__ . ‘方法‘ . PHP_EOL;        echo print_r(func_get_args(), true);        $this->container[$offset] = $value;    }    public function offsetUnset($offset)    {        echo ‘調用‘ . __METHOD__ . ‘方法‘ . PHP_EOL;        echo print_r(func_get_args(), true);        unset($this->container[$offset]);    }}//執行個體化對象$zhangsan = new Obj();//賦值$zhangsan[‘name‘] = ‘張三‘;//調用Obj::offsetSet方法//輸出echo $zhangsan[‘name‘] . PHP_EOL;//調用Obj::offsetGet方法//校正是否存在isset($zhangsan[‘name‘]) . PHP_EOL;//調用Obj::offsetExists方法//刪除數組單元unset($zhangsan[‘name‘]);//調用Obj::offsetUnset方法

應用案例示範

實現設定檔資訊讀取

準備工作:

按照一下給出的目錄結構建立檔案

./├── config│?? ├── app.php│?? └── database.php└── config.php
  • ./config/app.php

    <?phpreturn [    ‘name‘ => ‘app name‘,    ‘version‘ => ‘v1.0.0‘];
  • ./config/database.php

    <?phpreturn [    ‘mysql‘ => [        ‘host‘ => ‘localhost‘,        ‘user‘ => ‘root‘,        ‘password‘ => ‘12345678‘    ]];
  • ./config.php
<?phpnamespace Config;final Class Config implements \ArrayAccess{    private $config = [];    private static $instance = null;    private $path = null;    private function __construct()    {        $this->path = __DIR__ . ‘/config/‘;    }    //單例模式擷取執行個體    public static function getInstance()    {        if (!self::$instance instanceof self) {            self::$instance = new self;        }        return self::$instance;    }    //防止被複製    private function __clone()    {    }    public function offsetExists($offset)    {        return isset($this->config[$offset]);    }    public function offsetGet($offset)    {        if (!isset($this->config[$offset]) || 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::getInstance();//擷取app.php 檔案的 nameecho $config[‘app‘][‘name‘].PHP_EOL; //app name//擷取database.php檔案mysql的user配置echo $config[‘database‘][‘mysql‘][‘user‘].PHP_EOL; // root

PHP預定義介面:ArrayAccess

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.