2007年06月26日

SPL:Use ArrayObject and ArrayIterator to Overload Operators of Array

Tags: php spl arrayobject iterator overloading magic_method

If you want to design a class and make it's behavior as an array, you may extend ArrayObject. Also this new class probably need to use an instance of class which extends ArrayIterator (Note: ArrayIterator is a class, not an interface).

This feature requires PHP 5.


The statement foreach ($arrayobject as $k => $v) {...} means:

Note: A native array could be iterate by reference. For example: foreach ($array as $k => &$v). But an instance of ArrayIterator can not do that. Cause the value of iteration is return from method ArrayIterator::current() which prototype is not a reference.

The following codes shows how to overload indexer and foreach structure in PHP5.


Posted by shirock at 樂多Roodo! │11:37 │回應(0)引用(1)PHP
樂多分類:網路/3C 共同主題:PHP基本語法 工具:編輯本文
Ads by Roodo! 

引用URL

http://cgi.blog.roodo.com/trackback/3532653
引用列表:
實作一個 Stack ,示範如何覆載陣列相關的運算。
Stack - Example for Operators of Array Overload【石頭閒語】 at 2007年06月27日 17:08