濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > PHP數(shù)組式訪問(wèn)接口ArrayAccess用法分析

PHP數(shù)組式訪問(wèn)接口ArrayAccess用法分析

熱門(mén)標(biāo)簽:400電話申請(qǐng)方法收費(fèi) 離石地圖標(biāo)注 江蘇外呼電銷機(jī)器人報(bào)價(jià) 400電話辦理福州市 深圳外呼系統(tǒng)收費(fèi) 專業(yè)電話機(jī)器人批發(fā)商 長(zhǎng)沙crm外呼系統(tǒng)業(yè)務(wù) 電話機(jī)器人危險(xiǎn)嗎 南寧高頻外呼回?fù)芟到y(tǒng)哪家好

本文實(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)常用方法解析

標(biāo)簽:南京 曲靖 株洲 南昌 太原 興安盟 濱州 白酒營(yíng)銷

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP數(shù)組式訪問(wèn)接口ArrayAccess用法分析》,本文關(guān)鍵詞  PHP,數(shù)組,式,訪問(wèn),接口,ArrayAccess,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP數(shù)組式訪問(wèn)接口ArrayAccess用法分析》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于PHP數(shù)組式訪問(wèn)接口ArrayAccess用法分析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    友谊县| 肃北| 郁南县| 关岭| 彩票| 云南省| 资兴市| 来宾市| 青田县| 阿巴嘎旗| 姜堰市| 红河县| 怀远县| 读书| 启东市| 墨玉县| 滨海县| 南溪县| 游戏| 永泰县| 米林县| 昌图县| 和林格尔县| 吉隆县| 张家川| 读书| 嘉义市| 隆林| 瓮安县| 湟源县| 临洮县| 陇西县| 定西市| 龙里县| 化隆| 南江县| 玉环县| 库车县| 宝应县| 宜黄县| 邵阳市|