[TYPO3-dev] [TYPO3-ect] PHP5 and SPL
Rainer Kuhn
kuhn at punkt.de
Mon Mar 20 10:28:43 CET 2006
Elmar Hinz wrote:
> One of the most impressing and usefull parts that found their way into
> PHP5 is SPL. So that we can base our work on it. SPL is the akronym
> for Standard PHP Library and currently deals with arrays, objects and
> iterators. [3]
>
> What does SPL do? To abstract it: It defines and implements two or
> three important interfaces:
>
> Iterator: [4]
>
> current ()
> key ()
> next ()
> rewind ()
> valid ()
>
> ArrayAccess: [5][8]
>
> offsetExists ($offset)
> offsetGet ($offset)
> offsetSet ($offset, $value)
> offsetUnset ($offset)
> ( append($value) )
>
> If your object implements Iterator you can iterate it in foreach in
> PHP5, just like a usual array.
>
> foreach($myObject as $key => $value){
> do something ...
> }
There's one more very interesting alternative to iterate an object in
SPL: the IteratorAggregate Interface from the Zend engine classes
(http://www.php.net/~helly/php/ext/spl/interfaceIteratorAggregate.html).
If you would rather implement the Iterator separately from your
"collection" object, implementing Iterator Aggregate will allow you to
delegate the work of iteration to a separate class, while still enabling
you to use the collection inside a foreach loop. It only has one method
to implement, getIterator() (this returns an external Iterator for the
implementing object), and once it's implemented, any object can be
"iteratable".
Cheers,
Rainer
--
PGP Key Fingerprint: 79B1 E6CF A6EB 60A1 D1E4 E41E 4C1E F87F 7B40 126C
More information about the TYPO3-dev
mailing list