PHP 高级编程(5/5) - SPL 数组重载
2014-06-17 00:52
477 查看
ArrayAccess接口
ArrayAccess接口是对象的行为看起来像个数组,定义了四个方法。接口概要如下:
ArrayAccess接口自身没有提供计算书组重元素数量的功能,如果要计算数量可以通过实现Countble接口。这个接口包含了一个count()方法,并且返回元素的数量。
ArratObject 类介绍
ArrayObject 类是一个 ArrayAccess 接口的实现类,它提供了迭代功能,以及很多用来排序和处理数据的非常有用的方法。
ArrayAccess接口是对象的行为看起来像个数组,定义了四个方法。接口概要如下:
ArrayAccess { /* Methods */ abstract public boolean offsetExists ( mixed $offset ) abstract public mixed offsetGet ( mixed $offset ) abstract public void offsetSet ( mixed $offset , mixed $value ) abstract public void offsetUnset ( mixed $offset ) }
ArrayAccess接口自身没有提供计算书组重元素数量的功能,如果要计算数量可以通过实现Countble接口。这个接口包含了一个count()方法,并且返回元素的数量。
<?php class MyArray implements ArrayAccess { protected $_arr; public function __construct() { $this->_arr = array(); } public function offsetSet($offset, $value) { $this->_arr[$offset] = $value; } public function offsetGet($offset) { return $this->_arr[$offset]; } public function offsetExists($offset) { return array_key_exists($offset, $this->_arr); } public function offsetUnset($offset) { unset($this->_arr[$offset]); } } $MyArray = new MyArray(); $MyArray['first'] = 'test'; echo $MyArray['first']; unset($MyArray['first']); ?>
ArratObject 类介绍
ArrayObject 类是一个 ArrayAccess 接口的实现类,它提供了迭代功能,以及很多用来排序和处理数据的非常有用的方法。
ArrayObject implements IteratorAggregate , ArrayAccess , Serializable , Countable { /* Constants */ const integer STD_PROP_LIST = 1 ; const integer ARRAY_AS_PROPS = 2 ; /* Methods */ public __construct ([ mixed $input = [] [, int $flags = 0 [, string $iterator_class = "ArrayIterator" ]]] ) public void append ( mixed $value ) public void asort ( void ) public int count ( void ) public array exchangeArray ( mixed $input ) public array getArrayCopy ( void ) public int getFlags ( void ) public ArrayIterator getIterator ( void ) public string getIteratorClass ( void ) public void ksort ( void ) public void natcasesort ( void ) public void natsort ( void ) public bool offsetExists ( mixed $index ) public mixed offsetGet ( mixed $index ) public void offsetSet ( mixed $index , mixed $newval ) public void offsetUnset ( mixed $index ) public string serialize ( void ) public void setFlags ( int $flags ) public void setIteratorClass ( string $iterator_class ) public void uasort ( callable $cmp_function ) public void uksort ( callable $cmp_function ) public void unserialize ( string $serialized ) }
相关文章推荐
- PHP 高级编程(3/5) - 使用SPL(标准PHP库)实现观察者模式
- PHP编程之高级技巧――利用Mysql函数
- PHP编程中“数组”的基础知识
- php高级编程实例分析149(内部笔记)
- 【Objective-C高级编程】iOS与OS X多线程和内存管理(八) __block 从栈上复制到堆 截获对象 数组
- PHP编程之旅----变量、日期、数组、函数
- 最近碰到个问题,关于php扩展编程如何返回数组的问题
- PHP 高级编程(1/5) - 编码规范及文档编写
- 入门:PHP编程“数组”的基础知识
- 关于php扩展编程如何返回数组的问题
- php数组高级小结(一)
- 入门:PHP编程“数组”的基础知识
- PHP编程中数组的基础知识(转自http://info.codepub.com/2008/08/info-21593.html)
- PHP Mysql编程之高级技巧
- PHP对数组的高级遍历和操作处理方法
- PHP编程之高级技巧――利用Mysql函数
- PHP编程之高级技巧&#8212;&#8212;利用Mysql函数
- SPL学习笔记之四:数组重载
- php中重载数组操作符
- php学习高级-提高PHP编程效率的几点建议