Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 669535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:09:41+00:00 2026-05-14T00:09:41+00:00

PLEASE CHECK ANSWERS by VolkerK too, he provided another solution, but I can’t mark

  • 0

PLEASE CHECK ANSWERS by VolkerK too, he provided another solution, but I can’t mark two posts as answer. 🙁


Good day!

I know that C# allows multiple iterators using yield, like described here:
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'
         }
     }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:09:42+00:00Added an answer on May 14, 2026 at 12:09 am

    Yes, you can.

    foreach(new IteratorOne($obj) as $foo) ....
    
    foreach(new IteratorTwo($obj) as $bar) .....
    

    Actually, as long as you class implements Iterator, you can apply any arbitrary IteratorIterator to it. This is a Good Thing, because applied meta iterators are not required to know anything about the class in question.

    Consider, for example, an iterable class like this

    class JustList implements Iterator
    {
        function __construct() { $this->items = func_get_args(); }
        function rewind()      { return reset($this->items); }
        function current()     { return current($this->items); }
        function key()         { return key($this->items); }
        function next()        { return next($this->items); }
        function valid()       { return key($this->items) !== null; }
    }
    

    Let’s define some meta iterators

    class OddIterator extends FilterIterator {
        function accept() { return parent::current() % 2;  }
    }
    
    class EvenIterator extends FilterIterator {
        function accept() { return parent::current() % 2 == 0;  }
    }
    

    Now apply meta iterators to the base class:

     $list = new JustList(1, 2, 3, 4, 5, 6, 7, 8, 9);
    
     foreach(new OddIterator($list) as $p) echo $p;  // prints 13579
     foreach(new EvenIterator($list) as $p) echo $p; // prints 2468
    

    UPDATE: php has no inner classes, so you’re kinda out of luck here, without resorting to eval, at least. Your iterators need to be separate classes, which are aware of the baseclass structure. You can make it less harmful by providing methods in the base class that instantiate iterators behind the scenes:

     class TreeDepthFirstIterator implements Iterator 
     {
          function __construct($someTree).....
     }
    
    
     class Tree
     {
           function depthFirst() { return new TreeDepthFirstIterator($this); }
            ....
     }
    
    
     foreach($myTree->depthFirst() as $node).....
    

    Another option is to use lambdas instead of foreach. This is nicer and more flexible, requires php5.3 though:

     class Tree
     {
            function depthFirst($func) {
                  while($node = .....)
                    $func($node);
    
     .....
    
     $myTree->depthFirst(function($node) {
         echo $node->name;
     });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: Before reading this question and its answer, please check your input element has
Please check this code out it compiles and runs absolutely fine.. The question is
Please check the following example: http://www.esaer.com.br/csstest/ If the vertical scrollbar doesn't appear, please resize
Please check out the following func and its output void main() { Distance d1;
Please check my HTML below: <table cellpadding=0 cellpadding=0 border=0> <tr> <td> <div class=toogler>Demo1</div> </td>
Please check this: http://img62.imageshack.us/img62/3598/ieff.png why is it more height than in FF.. i want
Please check below screen shot. What is main difference between Master Page and MVC
Please check out http://rainsongmanchesters.net/news.htm and notice the black strip above the header image. Now
Please check out: http://gherkin.co.nz/refresh/ and click on the purple box that says 'Membership chest'
I have created a DeclarativeServiceLibrary using VS2010 beta 2, Please check this image of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.