Are Multiple Iterators possible in php?

Posted by artvolk on Stack Overflow See other posts from Stack Overflow or by artvolk
Published on 2010-03-14T11:24:38Z Indexed on 2010/03/14 13:45 UTC
Read the original article Hit count: 416

Filed under:
|
|

Good day!

I know that C# allows multiple iterators using yield, like described here: http://stackoverflow.com/questions/1754041/is-multiple-iterators-is-possible-in-c

In PHP there is and Iterator interface. Is it possible to implement more than one iteration scenario for a class?

More details (EDIT):

For example I have class TreeNode implementing single tree node. The whole tree can be expressed using only one this class. I want to provide iterators for iterating all direct and indirect children of current node, for example using BreadthFirst or DepthFirst order.

I can implement this Iterators as separate classes but doing so I need that tree node should expose it's children collection as public.

C# pseudocode:

 public class TreeNode<T> 
  {
     ...
     public IEnumerable<T> DepthFirstEnumerator
     {
         get
        {
            // Some tree traversal using 'yield return'
        }
     }

     public IEnumerable<T> BreadthFirstEnumerator
     {
         get
         {
             // Some tree traversal using 'yield return'
         }
     }
 }

© Stack Overflow or respective owner

Related posts about php

Related posts about design-patterns