ArrayAccess使用和配置

php by 黃業(yè)興 at 2020-04-07

提供像訪問數(shù)組一樣訪問對象的能力的接口

ArrayAccess {
    //檢查一個偏移位置是否存在
    abstract public boolean offsetExists ( mixed $offset );

    //獲取一個偏移位置的值
    abstract public mixed offsetGet ( mixed $offset );

    //設(shè)置一個偏移位置的值
    abstract public void offsetSet ( mixed $offset , mixed $value );

    //復(fù)位一個偏移位置的值
    abstract public void offsetUnset ( mixed $offset );
}

實例:

假如我有一個User類,映射的是用戶的信息,想通過數(shù)組的方式來訪問和設(shè)置用戶信息

class User implements \ArrayAccess
{
    private $data = [];

    public function __construct()
    {
        $this->data =  [
            'name' => 'mimi',
            'sex' => '男',
            'email' => '920224145@qq.com'
        ];
    }

    /**
     * 檢查指定字段數(shù)據(jù)是否存在
     *
     * @param $offset
     * @return bool
     */
    public function offsetExists($offset)
    {
        return isset($this->data[$offset]);
    }

    /**
     * 獲取指定字段數(shù)據(jù)
     *
     * @param $offset
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return $this->data[$offset];
    }

    /**
     * 設(shè)置指定字段數(shù)據(jù)
     *
     * @param $offset
     * @param $value
     * @return mixed
     */
    public function offsetSet($offset, $value)
    {
        return $this->data[$offset] = $value;
    }

    /**
     * 刪除指定字段數(shù)據(jù)
     *
     * @param $offset
     */
    public function offsetUnset($offset)
    {
        unset($this->data[$offset]);
    }
}

$user = new User();

//獲取用戶的email
echo $user['email'].PHP_EOL;  // 920224145@qq.com

//檢查age是否存在
var_dump(isset($user['age'])); // bool(false)

//設(shè)置age
$user['age'] = 18;
echo $user['age'].PHP_EOL; //18

//刪除age
unset($user['age']);
var_dump(isset($user['age'])); // bool(false)

實現(xiàn)程序配置化

  1. 在項目根目錄下創(chuàng)建一個config目錄 2. 在config目錄下創(chuàng)建相應(yīng)的配置文件,比如app.php 和 database.php。文件程序如下
==================================================================
return [
    'name' => 'app',
    'version' => 'v1'
];
==================================================================
return [
    'mysql' => [
        'host' => 'localhost',
        'user' => 'root',
        'password' => '12345678'
    ]
];
==================================================================

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('不提供設(shè)置配置');
    }

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

$config = Config::instance();
//獲取app.php 文件的 name
echo $config['app']['name'].PHP_EOL; //app
//獲取database.php文件mysql的user配置
echo $config['database']['mysql']['user'].PHP_EOL; // root

原來thinkphp配置文件是這樣配置的!

請關(guān)注我們微信公眾號:mw748219