本文實例講述了PHP數組式訪問接口ArrayAccess用法。分享給大家供大家參考,具體如下:
PHP ArrayAccess接口又叫數組式訪問接口,該接口的作用是提供像訪問數組一樣訪問對象的能力。
接口摘要如下:
- 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
- * 該類允許以數組或對象的方式進行訪問
- *
- * @author 瘋狂老司機
- */
- class ArrayAndObjectAccess implements ArrayAccess {
- /**
- * 定義一個數組用于保存數據
- *
- * @access private
- * @var array
- */
- private $data = [];
- /**
- * 以對象方式訪問數組中的數據
- *
- * @access public
- * @param string 數組元素鍵名
- */
- public function __get($key) {
- return $this->data[$key];
- }
- /**
- * 以對象方式添加一個數組元素
- *
- * @access public
- * @param string 數組元素鍵名
- * @param mixed 數組元素值
- * @return mixed
- */
- public function __set($key,$value) {
- $this->data[$key] = $value;
- }
- /**
- * 以對象方式判斷數組元素是否設置
- *
- * @access public
- * @param 數組元素鍵名
- * @return boolean
- */
- public function __isset($key) {
- return isset($this->data[$key]);
- }
- /**
- * 以對象方式刪除一個數組元素
- *
- * @access public
- * @param 數組元素鍵名
- */
- public function __unset($key) {
- unset($this->data[$key]);
- }
- /**
- * 以數組方式向data數組添加一個元素
- *
- * @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;
- }
- }
- /**
- * 以數組方式獲取data數組指定位置元素
- *
- * @access public
- * @abstracting ArrayAccess
- * @param 偏移位置
- * @return mixed
- */
- public function offsetGet($offset) {
- return $this->offsetExists($offset) ? $this->data[$offset] : null;
- }
- /**
- * 以數組方式判斷偏移位置元素是否設置
- *
- * @access public
- * @abstracting ArrayAccess
- * @param 偏移位置
- * @return boolean
- */
- public function offsetExists($offset) {
- return isset($this->data[$offset]);
- }
- /**
- * 以數組方式刪除data數組指定位置元素
- *
- * @access public
- * @abstracting ArrayAccess
- * @param 偏移位置
- */
- public function offsetUnset($offset) {
- if ($this->offsetExists($offset)) {
- unset($this->data[$offset]);
- }
- }
- } //Vevb.com
- $animal = new ArrayAndObjectAccess();
- $animal->dog = 'dog'; // 調用ArrayAndObjectAccess::__set
- $animal['pig'] = 'pig'; // 調用ArrayAndObjectAccess::offsetSet
- var_dump(isset($animal->dog)); // 調用ArrayAndObjectAccess::__isset
- var_dump(isset($animal['pig'])); // 調用ArrayAndObjectAccess::offsetExists
- var_dump($animal->pig); // 調用ArrayAndObjectAccess::__get
- var_dump($animal['dog']); // 調用ArrayAndObjectAccess::offsetGet
- unset($animal['dog']); // 調用ArrayAndObjectAccess::offsetUnset
- unset($animal->pig); // 調用ArrayAndObjectAccess::__unset
- var_dump($animal['pig']); // 調用ArrayAndObjectAccess::offsetGet
- var_dump($animal->dog); // 調用ArrayAndObjectAccess::__get
- ?>
以上輸出:
- boolean true
- boolean true
- string 'pig' (length=3)
- string 'dog' (length=3)
- null
- null
新聞熱點
疑難解答