本文實(shí)例講述了PHP數(shù)組式訪問(wèn)接口ArrayAccess用法。分享給大家供大家參考,具體如下:
PHP ArrayAccess接口又叫數(shù)組式訪問(wèn)接口,該接口的作用是提供像訪問(wèn)數(shù)組一樣訪問(wèn)對(duì)象的能力。
接口摘要如下:
ArrayAccess {
// 獲取一個(gè)偏移位置的值
abstract public mixed offsetGet ( mixed $offset )
// 設(shè)置一個(gè)偏移位置的值
abstract public void offsetSet ( mixed $offset , mixed $value )
// 檢查一個(gè)偏移位置是否存在
abstract public boolean offsetExists ( mixed $offset )
// 復(fù)位一個(gè)偏移位置的值
abstract public void offsetUnset ( mixed $offset )
}
例子說(shuō)明:
?php
/**
* ArrayAndObjectAccess
* 該類允許以數(shù)組或?qū)ο蟮姆绞竭M(jìn)行訪問(wèn)
*
* @author 瘋狂老司機(jī)
*/
class ArrayAndObjectAccess implements ArrayAccess {
/**
* 定義一個(gè)數(shù)組用于保存數(shù)據(jù)
*
* @access private
* @var array
*/
private $data = [];
/**
* 以對(duì)象方式訪問(wèn)數(shù)組中的數(shù)據(jù)
*
* @access public
* @param string 數(shù)組元素鍵名
*/
public function __get($key) {
return $this->data[$key];
}
/**
* 以對(duì)象方式添加一個(gè)數(shù)組元素
*
* @access public
* @param string 數(shù)組元素鍵名
* @param mixed 數(shù)組元素值
* @return mixed
*/
public function __set($key,$value) {
$this->data[$key] = $value;
}
/**
* 以對(duì)象方式判斷數(shù)組元素是否設(shè)置
*
* @access public
* @param 數(shù)組元素鍵名
* @return boolean
*/
public function __isset($key) {
return isset($this->data[$key]);
}
/**
* 以對(duì)象方式刪除一個(gè)數(shù)組元素
*
* @access public
* @param 數(shù)組元素鍵名
*/
public function __unset($key) {
unset($this->data[$key]);
}
/**
* 以數(shù)組方式向data數(shù)組添加一個(gè)元素
*
* @access public
* @abstracting ArrayAccess
* @param string 偏移位置
* @param mixed 元素值
*/
public function offsetSet($offset,$value) {
if (is_null($offset)) {
$this->data[] = $value;
} else {
$this->data[$offset] = $value;
}
}
/**
* 以數(shù)組方式獲取data數(shù)組指定位置元素
*
* @access public
* @abstracting ArrayAccess
* @param 偏移位置
* @return mixed
*/
public function offsetGet($offset) {
return $this->offsetExists($offset) ? $this->data[$offset] : null;
}
/**
* 以數(shù)組方式判斷偏移位置元素是否設(shè)置
*
* @access public
* @abstracting ArrayAccess
* @param 偏移位置
* @return boolean
*/
public function offsetExists($offset) {
return isset($this->data[$offset]);
}
/**
* 以數(shù)組方式刪除data數(shù)組指定位置元素
*
* @access public
* @abstracting ArrayAccess
* @param 偏移位置
*/
public function offsetUnset($offset) {
if ($this->offsetExists($offset)) {
unset($this->data[$offset]);
}
}
}
$animal = new ArrayAndObjectAccess();
$animal->dog = 'dog'; // 調(diào)用ArrayAndObjectAccess::__set
$animal['pig'] = 'pig'; // 調(diào)用ArrayAndObjectAccess::offsetSet
var_dump(isset($animal->dog)); // 調(diào)用ArrayAndObjectAccess::__isset
var_dump(isset($animal['pig'])); // 調(diào)用ArrayAndObjectAccess::offsetExists
var_dump($animal->pig); // 調(diào)用ArrayAndObjectAccess::__get
var_dump($animal['dog']); // 調(diào)用ArrayAndObjectAccess::offsetGet
unset($animal['dog']); // 調(diào)用ArrayAndObjectAccess::offsetUnset
unset($animal->pig); // 調(diào)用ArrayAndObjectAccess::__unset
var_dump($animal['pig']); // 調(diào)用ArrayAndObjectAccess::offsetGet
var_dump($animal->dog); // 調(diào)用ArrayAndObjectAccess::__get
?>
以上輸出:
boolean true
boolean true
string 'pig' (length=3)
string 'dog' (length=3)
null
null
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《php常用函數(shù)與技巧總結(jié)》、《PHP錯(cuò)誤與異常處理方法總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- 基于php雙引號(hào)中訪問(wèn)數(shù)組元素報(bào)錯(cuò)的解決方法
- php訪問(wèn)數(shù)組最后一個(gè)元素的函數(shù)end()用法
- PHP 的ArrayAccess接口 像數(shù)組一樣來(lái)訪問(wèn)你的PHP對(duì)象
- PHP如何使用array_unshift()在數(shù)組開(kāi)頭插入元素
- PHP數(shù)組Key強(qiáng)制類型轉(zhuǎn)換實(shí)現(xiàn)原理解析
- PHP讀取遠(yuǎn)程txt文檔到數(shù)組并實(shí)現(xiàn)遍歷
- PHP基于array_unique實(shí)現(xiàn)二維數(shù)組去重
- PHP二維數(shù)組分頁(yè)2種實(shí)現(xiàn)方法解析
- PHP數(shù)組訪問(wèn)常用方法解析