Iterating PHP Iterators

Review

Christopher Pitt
3 min readApr 7, 2015

Cal Evans recently asked me if I would like to review his new book; “Iterating PHP Iterators”. Naturally I jumped at the chance to learn more from Cal, and his book didn’t disappoint. I thought I’d write a review, so others can find out what it’s about, before throwing money at it!

Content

The book begins gently, explaining what iterators are. Chapter 1 explains the difference between things that are iterable (like arrays) and things that are called Iterators. It makes a clear distinction between the kinds of iterators that developers implement and the kinds of iterators that developers use.

Chapter 2 focusses on why you would need to build any iterators (when the Standard Package Library already ships with 24 of them). The book shows how to create a simple iterator which is also countable.

Chapter 3 moves on to explain how the iterator works, internally. It’s a short but essential chapter in the understanding of these structures.

Chapter 4 starts to explain what Outer Iterators are and how they are made. There are many examples of Outer Iterators (in SPL) but they are seldom explained in this way. I didn’t see the term Decorator mentioned once, but that’s what these constructs are. Still they are explained simply.

Chapter 5 touches on the use of ArrayIterator, and how it exposes array-like functionality through the 4 offset* methods. It’s also the first mention of (bitwise) flags. The subject of flags is also mentioned later on.

Chapter 6 explains how to modifying output — modifying current() to return unexpected values during iteration. It also talks about restricting input by using Output Iterators as filters.

Chapters 7 and 8 are short and focussed on seeking and recursion. Unlike how most iterators are used, seeking allows random access to any position in the iterator, without iterating to that point. Recursion allows for things like iterating over nested arrays or other complex data structures.

Chapter 9 takes a step back to demonstrate how iterators can be used for file system iteration. This chapter is a little out of place, being quite practical in-between theoretical chapters. Still, it’s useful to see a technically sound method for something developers frequently botch up!

Chapter 10 looks at the CachingIterator. I didn’t really enjoy this chapter as much, since it’s more a warning of why not to use this iterator than anything else. It’s a strange class, and without many practical examples, I battle to see why it would be worth the potential bugs it could create.

Chapter 11 completes the formal part of the book with a short and sharp look at generators. It’s not the most comprehensive look at generators, but it’s accurate. I would have liked to see more about this powerful language construct.

The book also includes an appendix on bitwise flags; how to understand and use them. It’s actually one of the most practically useful sections of the book.

Style

The book is generally easy to read. Some of the sentences are a little long and/or complex, but nothing prohibitively so. I enjoy Cal’s writing, and if you’ve read his other books then you’ll feel right at home in this one.

Length

This is a short book. 65 pages, 9 of which are “other stuff”. That’s not necessarily bad, since it manages to explain a lot about iterators.

It does feel a little too short though, especially in sections like recursion (3 pages) and generators (4 pages). I think it would have been better to drop the CacheIterator chapter and expand these chapters.

Conclusion

This is a great book for beginners and intermediate developers. It’s a comprehensive introduction to iterators and some of the implementations that ship with the SPL. Advanced developers (or those that have spent much time with iterators) might find it a little simplistic or short.

It’s also about to be launched (around the first week of March, 2015) for $5.99. After a week the price goes up to $9.95, so it would be best to get it before the price goes up. I’m definitely going to recommend SilverStripe gets this for junior developers.

--

--