您好,登錄后才能下訂單哦!
簡(jiǎn)介
- 提供像訪問(wèn)數(shù)組一樣訪問(wèn)對(duì)象的能力的接口。
接口摘要
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 — 檢查一個(gè)偏移位置是否存在
- ArrayAccess::offsetGet — 獲取一個(gè)偏移位置的值
- ArrayAccess::offsetSet — 設(shè)置一個(gè)偏移位置的值
- ArrayAccess::offsetUnset — 復(fù)位一個(gè)偏移位置的值
代碼演示
class Obj implements ArrayAccess
{
private $container = [];
public function offsetExists($offset): bool
{
echo '調(diào)用' . __METHOD__ . '方法' . PHP_EOL;
echo print_r(func_get_args(), true);
return isset($this->container[$offset]);
}
public function offsetGet($offset)
{
echo '調(diào)用' . __METHOD__ . '方法' . PHP_EOL;
echo print_r(func_get_args(), true);
return $this->container[$offset] ?? null;
}
public function offsetSet($offset, $value)
{
echo '調(diào)用' . __METHOD__ . '方法' . PHP_EOL;
echo print_r(func_get_args(), true);
$this->container[$offset] = $value;
}
public function offsetUnset($offset)
{
echo '調(diào)用' . __METHOD__ . '方法' . PHP_EOL;
echo print_r(func_get_args(), true);
unset($this->container[$offset]);
}
}
//實(shí)例化對(duì)象
$zhangsan = new Obj();
//賦值
$zhangsan['name'] = '張三';//調(diào)用Obj::offsetSet方法
//輸出
echo $zhangsan['name'] . PHP_EOL;//調(diào)用Obj::offsetGet方法
//校驗(yàn)是否存在
isset($zhangsan['name']) . PHP_EOL;//調(diào)用Obj::offsetExists方法
//刪除數(shù)組單元
unset($zhangsan['name']);//調(diào)用Obj::offsetUnset方法
通過(guò)調(diào)用接口方法來(lái)實(shí)現(xiàn)對(duì)對(duì)象屬性進(jìn)行操作,然后使用數(shù)組的方式訪問(wèn)
$zhangsan=new Obj();
if ($zhangsan instanceof \ArrayAccess) {
//通過(guò)調(diào)用接口方法為對(duì)象賦值
$zhangsan->offsetSet('name', '張三');
//通過(guò)數(shù)組方式取值
echo $zhangsan['name'] . PHP_EOL;
//通過(guò)調(diào)用接口方法取值
echo $zhangsan->offsetGet('name').PHP_EOL;
//通過(guò)調(diào)用接口方法來(lái)判斷對(duì)象屬性是否存在
if (!$zhangsan->offsetExists('age') && !isset($zhangsan['age'])) {
$zhangsan->offsetSet('age', '18');
}
$zhangsan['address']='北京';
//通過(guò)調(diào)用接口方法來(lái)判斷對(duì)象屬性
$zhangsan->offsetUnset('address');
echo $zhangsan['address'] ?? 'address is not exists';
//直接為對(duì)象屬性賦值(不可行)
$zhangsan->school='Peking University';
echo $zhangsan['school'] ?? 'school is not exists';//school is not exists
}
應(yīng)用案例演示
實(shí)現(xiàn)配置文件信息讀取
按照一下給出的目錄結(jié)構(gòu)建立文件
./
├── config
│?? ├── app.php
│?? └── database.php
└── config.php
./config/app.php
<?php
return [
'name' => 'app name',
'version' => 'v1.0.0'
];
./config/database.php
<?php
return [
'mysql' => [
'host' => 'localhost',
'user' => 'root',
'password' => '12345678'
]
];
<?php
namespace Config;
final Class Config implements \ArrayAccess
{
private $config = [];
private static $instance = null;
private $path = null;
private function __construct()
{
$this->path = __DIR__ . '/config/';
}
//單例模式獲取實(shí)例
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('不提供設(shè)置配置');
}
public function offsetUnset($offset)
{
throw new \Exception('不提供刪除配置');
}
}
$config = Config::getInstance();
//獲取app.php 文件的 name
echo $config['app']['name'].PHP_EOL; //app name
//獲取database.php文件mysql的user配置
echo $config['database']['mysql']['user'].PHP_EOL; // root
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。