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

Jeremy Cook's Blog:
Implementing IteratorAggregate and Iterator
May 14, 2012 @ 18: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.

tagged: iteratoraggregate iterator tutorial iterate traversable

Link:

Till Klampaeckel's Blog:
Iterating over a table (with Zend_Db_Table and Zend_Paginator)
Oct 12, 2011 @ 16:01:44

Till Klampaeckel has a new post today looking at a solution for a common need - paginating through results as pulled from a database. With the help of the Zend_Db_Table and Zend_Paginator components of the Zend Framework it's a simple matter of passing the results into the Paginator and asking for a certain page.

So frequently, I need to run small data migrations or transformations. Especially on the way to Doctrine, there's a lot to clean-up in a database which has been used and evolved over five years or so.

Code snippets are included to define a class for the table, extending Zend_Db_Table_Abstract, and a new Zend_Paginator_Adapter_DbTableSelect object to create the paginated results. After that, it's as simple as setting the number of items per page and asking for a certain page. There's even a quick bit about being able to edit the rows inside the paginator directly (they're just Zend_Db_Table_Row records).

tagged: tutorial iterate table zendframework zenddbtable zendpaginator

Link:

Christian Schaefer's Blog:
Simply iterate over XML with plain PHP using little memory and CPU
Mar 10, 2011 @ 14:11:31

In a new post to his Test.ical.ly blog Christian Schaefer shows you how to iterate over XML in a more efficient way with the help of the XMLReader and Iterator features that come with PHP.

One of the things I have been working on lately was a simple XML parser. It’s a simple XML structure in my case though it could be more complex without much change. My solution was a quite powerful yet simple combination of XMLReader and the Iterator interface.

He includes a sample XML document similar to the one he was working with and shows how XMLReader can handle it, keeping only the currently needed information in memory at one time. His sample class (CustomXml) loads the file and defines all of the iterator methods to work with the data like "next", "prev" and "rewind".

tagged: iterate xml xmlreader iterator memory tutorial

Link:

Matthew Weier O'Phinney's Blog:
Taming SplPriorityQueue
Jan 18, 2011 @ 18:43:54

Matthew Weier O'Phinney has a new post to his blog today looking at one of the tools the Standard PHP Library (SPL) has to offer developers - the SplPriorityQueue (PHP 5.3+)

SplPriorityQueue is a fantastic new feature of PHP 5.3. However, in trying to utilize it in a few projects recently, I've run into some behavior that's (a) non-intuitive, and (b) in some cases at least, undesired. In this post, I'll present my solutions.

He talks about the "first in, first out" nature of queues and how it differs from a stack (including links to some of the other SPL offerings for both). He then moves into the problems he was seeing - that iteration removes values from the heap and the unexpected order of equal values in the queue. To solve the first problem, he creates an "outer iterator" that creates an "innerQueue" that's protected. The solution for the second issue - the random queue order - is a simple one: priority indexes aren't required to be integers. Strings can be substituted to help make things a bit more unique.

tagged: splpriorityqueue heap stack queue spl tutorial iterate priority index

Link:

Matthew Turland's Blog:
A Few Kinks in FilterIterator
Aug 16, 2010 @ 15:18:40

In this quick post to his blog Matthew Turland shares a "kink" he found in using the FilterIteractor SPL iterator when working with the Phergie project's code.

Once I discovered the segfault [from the FilterIterator code], I had to come up with a short code sample exposing the bug in order to report it.

He talks about the bug that led him to the segfault and a second bug that was a side effect of the first causing the first element to be skipped during iteration.

tagged: filteriterator spl bug iterate segfault

Link:


Trending Topics: