国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP數組式訪問接口ArrayAccess用法分析

2024-09-04 11:46:42
字體:
來源:轉載
供稿:網友

本文實例講述了PHP數組式訪問接口ArrayAccess用法。分享給大家供大家參考,具體如下:

PHP  ArrayAccess接口又叫數組式訪問接口,該接口的作用是提供像訪問數組一樣訪問對象的能力。

接口摘要如下:

  1. ArrayAccess { 
  2.   // 獲取一個偏移位置的值 
  3.   abstract public mixed offsetGet ( mixed $offset ) 
  4.   // 設置一個偏移位置的值 
  5.   abstract public void offsetSet ( mixed $offset , mixed $value ) 
  6.   // 檢查一個偏移位置是否存在 
  7.   abstract public boolean offsetExists ( mixed $offset ) 
  8.   // 復位一個偏移位置的值 
  9.   abstract public void offsetUnset ( mixed $offset ) 

例子說明:

  1. <?php 
  2. /** 
  3. * ArrayAndObjectAccess 
  4. * 該類允許以數組或對象的方式進行訪問 
  5. * 
  6. * @author 瘋狂老司機 
  7. */ 
  8. class ArrayAndObjectAccess implements ArrayAccess { 
  9.   /** 
  10.    * 定義一個數組用于保存數據 
  11.    * 
  12.    * @access private 
  13.    * @var array 
  14.    */ 
  15.   private $data = []; 
  16.   /** 
  17.    * 以對象方式訪問數組中的數據 
  18.    * 
  19.    * @access public 
  20.    * @param string 數組元素鍵名 
  21.    */ 
  22.   public function __get($key) { 
  23.     return $this->data[$key]; 
  24.   } 
  25.   /** 
  26.    * 以對象方式添加一個數組元素 
  27.    * 
  28.    * @access public 
  29.    * @param string 數組元素鍵名 
  30.    * @param mixed 數組元素值 
  31.    * @return mixed 
  32.    */ 
  33.   public function __set($key,$value) { 
  34.     $this->data[$key] = $value
  35.   } 
  36.   /** 
  37.    * 以對象方式判斷數組元素是否設置 
  38.    * 
  39.    * @access public 
  40.    * @param 數組元素鍵名 
  41.    * @return boolean 
  42.    */ 
  43.   public function __isset($key) { 
  44.     return isset($this->data[$key]); 
  45.   } 
  46.   /** 
  47.    * 以對象方式刪除一個數組元素 
  48.    * 
  49.    * @access public 
  50.    * @param 數組元素鍵名 
  51.    */ 
  52.   public function __unset($key) { 
  53.     unset($this->data[$key]); 
  54.   } 
  55.   /** 
  56.    * 以數組方式向data數組添加一個元素 
  57.    * 
  58.    * @access public 
  59.    * @abstracting ArrayAccess 
  60.    * @param string 偏移位置 
  61.    * @param mixed 元素值 
  62.    */ 
  63.   public function offsetSet($offset,$value) { 
  64.     if (is_null($offset)) { 
  65.       $this->data[] = $value
  66.     } else { 
  67.       $this->data[$offset] = $value
  68.     } 
  69.   } 
  70.   /** 
  71.    * 以數組方式獲取data數組指定位置元素 
  72.    * 
  73.    * @access public 
  74.    * @abstracting ArrayAccess 
  75.    * @param 偏移位置 
  76.    * @return mixed 
  77.    */ 
  78.   public function offsetGet($offset) { 
  79.     return $this->offsetExists($offset) ? $this->data[$offset] : null; 
  80.   } 
  81.   /** 
  82.    * 以數組方式判斷偏移位置元素是否設置 
  83.    * 
  84.    * @access public 
  85.    * @abstracting ArrayAccess 
  86.    * @param 偏移位置 
  87.    * @return boolean 
  88.    */ 
  89.   public function offsetExists($offset) { 
  90.     return isset($this->data[$offset]); 
  91.   } 
  92.   /** 
  93.    * 以數組方式刪除data數組指定位置元素 
  94.    * 
  95.    * @access public 
  96.    * @abstracting ArrayAccess 
  97.    * @param 偏移位置 
  98.    */ 
  99.   public function offsetUnset($offset) { 
  100.     if ($this->offsetExists($offset)) { 
  101.       unset($this->data[$offset]); 
  102.     } 
  103.   } 
  104. //Vevb.com 
  105. $animal = new ArrayAndObjectAccess(); 
  106. $animal->dog = 'dog'// 調用ArrayAndObjectAccess::__set 
  107. $animal['pig'] = 'pig'// 調用ArrayAndObjectAccess::offsetSet 
  108. var_dump(isset($animal->dog)); // 調用ArrayAndObjectAccess::__isset 
  109. var_dump(isset($animal['pig'])); // 調用ArrayAndObjectAccess::offsetExists 
  110. var_dump($animal->pig); // 調用ArrayAndObjectAccess::__get 
  111. var_dump($animal['dog']); // 調用ArrayAndObjectAccess::offsetGet 
  112. unset($animal['dog']); // 調用ArrayAndObjectAccess::offsetUnset 
  113. unset($animal->pig); // 調用ArrayAndObjectAccess::__unset 
  114. var_dump($animal['pig']); // 調用ArrayAndObjectAccess::offsetGet 
  115. var_dump($animal->dog); // 調用ArrayAndObjectAccess::__get 
  116. ?> 

以上輸出:

  1. boolean true 
  2. boolean true 
  3. string 'pig' (length=3) 
  4. string 'dog' (length=3) 
  5. null 
  6. null 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宿松县| 武宣县| 建德市| 凤凰县| 子洲县| 三亚市| 于田县| 古浪县| 保德县| 淮安市| 定西市| 盖州市| 阿拉尔市| 九江县| 洪雅县| 革吉县| 瓮安县| 新津县| 东安县| 繁昌县| 武功县| 开化县| 溆浦县| 彝良县| 浦东新区| 喀什市| 保亭| 上蔡县| 孝昌县| 雅安市| 定陶县| 安阳县| 梅河口市| 乌海市| 百色市| 会昌县| 商水县| 潼南县| 原平市| 恭城| 墨脱县|