PHP数组式访问接口ArrayAccess用法分析

yipeiwu_com6年前PHP代码库

本文实例讲述了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]);
    }
  }
}
$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

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《PHP常用遍历算法与技巧总结》、《php字符串(string)用法总结》、《php常用函数与技巧总结》、《PHP错误与异常处理方法总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP4与PHP5的时间格式问题

自己电脑上用的PHP5. 服务器上PHP4. 自己机器上时间,2008-2-14 14:56:29 写好程序传上去,变成一串数字。1202972189 数据库用的access....

使用php来实现网络服务

作者:samisa 以下文中的翻译名称对照表 : payload: 交谈内容 object: 实例 function: 函数 使用 php来实现网络服务 使用框架: WSO2 WSF/P...

PHP执行Curl时报错提示CURL ERROR: Recv failure: Connection reset by peer的解决方法

最近在使用curl中遇到CURL ERROR: Recv failure: Connection reset by peer的报错提示,现把解决方法与大家共享,希望对大家有所帮助。 我们...

PHP 时间日期操作实战

常见常用的时间函数: 1.time(); //取得1970/1/1 00:00:00 到现在的总秒数 <?echo time();?> 2.mktime(); //设定时间...

PHP代码审核的详细介绍

PHP代码审核的详细介绍

概述代码审核,是对应用程序源代码进行系统性检查的工作。它的目的是为了找到并且修复应用程序在开发阶段存在的一些漏洞或者程序逻辑错误,避免程序漏洞被非法利用给企业带来不必要的风险代码审核不是...