News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

PHPMaster.com:
Using SPL Iterators, Part 2
May 22, 2012 @ 08:23:17

On PHPMaster.com today they've posted the second part of the series covering the Iterators that come with PHP as a part of the SPL.

In part one of this series I introduced you to some of the SPL Iterators and how to use them. There may be times however when the provided iterators are insufficient for your needs and you'll want to roll your own custom iterator. Luckily, SPL provides interfaces that will allow you to do just that. For an object to be traversable in a foreach loop, PHP requires that it be an instance of Traversable. You cannot however implement this interface directly (though you can use it in instaceof checks); instead you'll need to implement either SPL's Iterator or IteratorAggregate interfaces.

He shows you how to implement these two interfaces in your own custom classes, looping through a set of books for the Iterator example and a "getIterator" method that creates an ArrayIterator when executed. The results of both are used in foreach loops showing how they can be used just like any other iteratable variables.

0 comments voice your opinion now!
spl iterators tutorial array iterator iteratoraggregate foreach


blog comments powered by Disqus

Similar Posts

DevShed: Tracking a Stack of Function Calls with the Xdebug Extension

Script-Tutorials.com: Vimeo API - OAuth and Upload Example

Sean Coates' Blog: A Case of Mistaken Iterator

Sudheer Satyanarayana's Blog: A Bit Of XML, RSS And CURL In 7 Lines Of PHP And A Useful Program

Wokay.com: 12+ Tutorials for creating PHP5 MVC Framework


Community Events





Don't see your event here?
Let us know!


opinion version laravel composer language framework voicesoftheelephpant release unittest community symfony configure podcast interview series threedevsandamaybe introduction list api conference

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework