PHP數(shù)組式訪問接口ArrayAccess用法分析
本文實(shí)例講述了PHP數(shù)組式訪問接口ArrayAccess用法。分享給大家供大家參考,具體如下:
PHP ArrayAccess接口又叫數(shù)組式訪問接口,該接口的作用是提供像訪問數(shù)組一樣訪問對象的能力。
接口摘要如下:
ArrayAccess { // 獲取一個偏移位置的值 abstract public mixed offsetGet ( mixed $offset ) // 設(shè)置一個偏移位置的值 abstract public void offsetSet ( mixed $offset , mixed $value ) // 檢查一個偏移位置是否存在 abstract public boolean offsetExists ( mixed $offset ) // 復(fù)位一個偏移位置的值 abstract public void offsetUnset ( mixed $offset ) }
例子說明:
<?php /** * ArrayAndObjectAccess * 該類允許以數(shù)組或?qū)ο蟮姆绞竭M(jìn)行訪問 * * @author 瘋狂老司機(jī) */ 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ù)組元素是否設(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ù)組方式判斷偏移位置元素是否設(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錯誤與異常處理方法總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
- 基于php雙引號中訪問數(shù)組元素報錯的解決方法
- php訪問數(shù)組最后一個元素的函數(shù)end()用法
- PHP 的ArrayAccess接口 像數(shù)組一樣來訪問你的PHP對象
- PHP如何使用array_unshift()在數(shù)組開頭插入元素
- 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ù)組分頁2種實(shí)現(xiàn)方法解析
- PHP數(shù)組訪問常用方法解析
相關(guān)文章
Json_decode 解析json字符串為NULL的解決方法(必看)
下面小編就為大家?guī)硪黄狫son_decode 解析json字符串為NULL的解決方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02PHP實(shí)現(xiàn)RSA簽名生成訂單功能【支付寶示例】
這篇文章主要介紹了PHP實(shí)現(xiàn)RSA簽名生成訂單功能,涉及php隨機(jī)字符串及編碼相關(guān)操作技巧,以及支付寶公鑰文件讀取與使用方法,需要的朋友可以參考下2017-06-06PHP生成指定范圍內(nèi)的N個不重復(fù)的隨機(jī)數(shù)
今天小編就為大家分享一篇關(guān)于PHP生成指定范圍內(nèi)的N個不重復(fù)的隨機(jī)數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03使用GDB調(diào)試PHP代碼,解決PHP代碼死循環(huán)問題
這篇文章主要介紹了使用GDB調(diào)試PHP代碼,解決PHP代碼死循環(huán)問題,需要的朋友可以參考下2015-03-03