本文實例講述了PHP數(shù)組式訪問接口ArrayAccess用法。分享給大家供大家參考,具體如下:
PHP ArrayAccess接口又叫數(shù)組式訪問接口,該接口的作用是提供像訪問數(shù)組一樣訪問對象的能力。
接口摘要如下:
ArrayAccess {
// 獲取一個偏移位置的值
abstract public mixed offsetGet ( mixed $offset )
// 設置一個偏移位置的值
abstract public void offsetSet ( mixed $offset , mixed $value )
// 檢查一個偏移位置是否存在
abstract public boolean offsetExists ( mixed $offset )
// 復位一個偏移位置的值
abstract public void offsetUnset ( mixed $offset )
}
例子說明:
?php
/**
* ArrayAndObjectAccess
* 該類允許以數(shù)組或對象的方式進行訪問
*
* @author 瘋狂老司機
*/
class ArrayAndObjectAccess implements ArrayAccess {
/**
* 定義一個數(shù)組用于保存數(shù)據(jù)
*
* @access private
* @var array
*/
private $data = [];
/**
* 以對象方式訪問數(shù)組中的數(shù)據(jù)
*
* @access public
* @param string 數(shù)組元素鍵名
*/
public function __get($key) {
return $this->data[$key];
}
/**
* 以對象方式添加一個數(shù)組元素
*
* @access public
* @param string 數(shù)組元素鍵名
* @param mixed 數(shù)組元素值
* @return mixed
*/
public function __set($key,$value) {
$this->data[$key] = $value;
}
/**
* 以對象方式判斷數(shù)組元素是否設置
*
* @access public
* @param 數(shù)組元素鍵名
* @return boolean
*/
public function __isset($key) {
return isset($this->data[$key]);
}
/**
* 以對象方式刪除一個數(shù)組元素
*
* @access public
* @param 數(shù)組元素鍵名
*/
public function __unset($key) {
unset($this->data[$key]);
}
/**
* 以數(shù)組方式向data數(shù)組添加一個元素
*
* @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ù)組方式判斷偏移位置元素是否設置
*
* @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
更多關于PHP相關內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結》、《php字符串(string)用法總結》、《php常用函數(shù)與技巧總結》、《PHP錯誤與異常處理方法總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:- 基于php雙引號中訪問數(shù)組元素報錯的解決方法
- php訪問數(shù)組最后一個元素的函數(shù)end()用法
- PHP 的ArrayAccess接口 像數(shù)組一樣來訪問你的PHP對象
- PHP如何使用array_unshift()在數(shù)組開頭插入元素
- PHP數(shù)組Key強制類型轉換實現(xiàn)原理解析
- PHP讀取遠程txt文檔到數(shù)組并實現(xiàn)遍歷
- PHP基于array_unique實現(xiàn)二維數(shù)組去重
- PHP二維數(shù)組分頁2種實現(xiàn)方法解析
- PHP數(shù)組訪問常用方法解析