2012年10月10日星期三

Web Mozarts: Give the Traversable Interface Some Love


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.

没有评论:

发表评论