欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年12月28日 09:57:05   作者:奔跑的碼農(nóng)  
這篇文章主要介紹了PHP數(shù)組式訪問接口ArrayAccess用法,結(jié)合實(shí)例形式分析了數(shù)組式訪問接口ArrayAccess的概念、功能、實(shí)現(xiàn)與使用方法,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

最新評論