News Feed
Sections




News Archive
feed this:

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

Web Mozarts:
Give the Traversable Interface Some Love
October 10, 2012 @ 09:12:56

In this recent post to the Web Mozarts site, Bernhard Schussek "gives Traversable some love" and introduces you to the Traversable interface and how it might work better for certain things than an Iterator.

Let's start with a simple use case. Let's create an interface ArrayInterface that demarcates objects that behave like PHP arrays. The interface should allow for counting, iterating and array access.

He shows how to create this interface based off of a "ArrayInterface" that implements "Countable", "ArrayAccess" and "Iterator" with all of the methods required for each. He points out that, while the documentation in the manual makes "Traversable" shouldn't be used, it can be extended instead of Iterator. This gives other classes that extend this interface the option of extending either of the Iterators ("Iterator" or "IteratorAggregate") they want.

0 comments voice your opinion now!
interface traversable introduction iterator iteratoraggregate


Jeremy Cook's Blog:
Implementing IteratorAggregate and Iterator
May 14, 2012 @ 13:04:58

In a recent post to his blog Jeremy Cook has gotten back into looking at some of the SPL functionality that comes with PHP. In this new post he looks specifically at the IteratorAggregate and Iterator object types.

After a bit of a break I'm finally able to get back to writing about the predefined interfaces in PHP. PHP provides two interfaces that allow you to define how your objects behave in a foreach loop: IteratorAggregate and Iterator. Before taking a look at IteratorAggregate I'll briefly discuss how we can iterate over objects in PHP 'natively' and what it means to be Traversable.

He introduces the concepts being being "iteratable" and "traversable". He then shows how to implement the IteratorAggregate (only one method required, "getIterator") and Iterator ("next", "valid", "current" and "key" methods required) in classes of your own.

You can find out more about these two object types (including more sample usage) on their manual pages: IteratorAggregate & Iterator.

0 comments voice your opinion now!
iteratoraggregate iterator tutorial iterate traversable


Lorna Mitchell's Blog:
Using iterator_to_array() in PHP
February 29, 2012 @ 08:55:52

Lorna Mitchell has a new post to her blog today showing off a lesser-known but very useful function included in PHP - the iterator_to_array function, used to translate things that implement Traversable into arrays.

Someone watching over my shoulder recently had never seen the ubiquitously-useful iterator_to_array() before. [...] Mostly I find this useful when I'm working with collections of data as these often present themselves as an object that you can foreach() over, but you can't dump it directly. If the object in question implements the Traversable interface, you can instead pass it into iterator_to_array to get the data as an array.

She includes a brief snippet of code showing it in use - transforming the results from a MongoDB cursor object back into an array.

0 comments voice your opinion now!
iterator translate array snippet traversable


Joshua Thijssen's Blog:
SPL Using the iteratorAggregate interface
December 06, 2011 @ 08:28:45

Joshua Thijssen has a recent post spotlighting a part of the Standard PHP Library (SPL) that implements that Traversable interface, the IteratorAggregate interface.

Together with its more famous brother "Iterator", they are currently the two only implementations of the "Traversable" interface, which is needed for objects so they can be used within a standard foreach() loop. But why and when should we use the iteratorAggregate?

He answers his question with an example - a book that contains chapters. With a normal iterator you'd have to define standard functions (like valid, rewind or key). Using the IteratorAggregate you can push items into an internal array (like chapters in a book) and call a "getIterator" method to get this set. He also takes it one step further and shows implementing the "Count" interface to make it easier to get a total count of the items in the iterator. Sample code is included to help clarify.

0 comments voice your opinion now!
spl iteratoraggregate interface traversable count


Fabien Potencier's Blog:
Iterator or IteratorAggregate?
June 25, 2010 @ 12:09:44

Following up on two previous posts about iterators, Fabien Potencier is back with one more quick shot on iterator aggregation - a look what using iterator versus iteratoraggregate.

If you have ever used iterators in your code, you have probably implemented the Iterator interface. Objects of a class that implements Iterator can be iterated over with the foreach loop. [...] The IteratorAggregate interface is quite similar [to Iterator] (both interfaces implement Traversable) but creates an external Iterator. But when the iterator is based on an array, creating an external Iterator for this array gives you a more concise and more readable code.

His example code shows how, in implementing and IteratorAggregate, you can grab the instance of the Iterator even if it's based on an array.

1 comment voice your opinion now!
iterator iteratoraggregate comparison traversable interface



Community Events





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


code framework language threedevsandamaybe series release introduction podcast community list laravel testing refactor developer interview experience opinion unittest symfony2 install

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