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 6775241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:53:01+00:00 2026-05-26T15:53:01+00:00

friends. I know, there are many questions here already on these iterators. I’ve read

  • 0

friends. I know, there are many questions here already on these iterators.
I’ve read something, and I’m not a beginner… but my mind is somewhat stuck on this. Please, help me to comprehend how I use iterators in practice.

Suppose, I have an ORM object that can select instances from database. And one instance contains fields and can insert, uodate etc. As usual.
I want to iterate through all objects of a type, but as there can be plenty of them, I prefer to select them by “pages”. My code:

$limit = 100;
$offset = 0;
do
{
  $recs = $orm->select($filter, $sorting, $limit , $offset);
  $offset += $limit;
  foreach ($recs as $rec)
  {
    // doing something with record
  }
}
while (count($recs) == $limit);

I feel that iterator paradigm is what suits here, but what interface is better to implement in this case or maybe some base SPL class?

UPDATE
Ideally code above with iterator may look like:

$iterator = new ORMPagedIterator($ormobject, $filter, $sorting);
foreach ($iterator as $rec)
{
  // do something with record
}

E.g. all that page by page behavior is inside the iterator.

  • 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-26T15:53:01+00:00Added an answer on May 26, 2026 at 3:53 pm

    I would use an Iterator that iterates over another Iterator and asks for a next Iterator once it reaches the end of the previous Iterator… ok, sounds a mo complicated than it actually is:

    <?php
    $limit = 100;
    $offset = 0;
    
    $iter = new NextIteratorCallbackIterator(function($i) use ($orm, $limit, &$offset) {
        printf("selecting next bunch at offset %d\n", $offset);
        $recs = $orm->select($filter, $sorting, $limit , $offset);
        $offset += $limit;
        if ($recs) {
            return new ArrayIterator($recs);
        }
        return null; // end reached
    });
    
    foreach ($iter as $rec) {
        // do something with record
    }
    ?>
    

    And here is a sample Implementation of that NextIteratorCallbackIterator:

    <?php
    class NextIteratorCallbackIterator implements Iterator {
        private $_iterator = null;
        private $_count = 0;
        private $_callback;
    
        public function __construct($callback) {
            if (!is_callable($callback)) {
                throw new Exception(__CLASS__.": callback must be callable");
            }
            $this->_callback = $callback;
        }
    
        public function current() {
            return $this->_iterator !== null ? $this->_iterator->current() : null;
        }
    
        public function key() {
            return $this->_iterator !== null ? $this->_iterator->key() : null;
        }
    
        public function next() {
            $tryNext = ($this->_iterator === null);
            do {
                if ($tryNext) {
                    $tryNext = false;
                    $this->_iterator = call_user_func($this->_callback, ++$this->_count);
                }
                elseif ($this->_iterator !== null) {
                    $this->_iterator->next();
                    if ($this->_iterator->valid() == false) {
                        $tryNext = true;
                    }
                }
            } while ($tryNext);
        }
    
        public function rewind() {
            $this->_iterator = call_user_func($this->_callback, $this->_count = 0);
        }
    
        public function valid () {
            return $this->_iterator !== null;
        }
    }
    ?>
    

    UPDATE: Your ORMPagedIterator can be implemented using NextIteratorCallbackIterator as easy as:

    <?php
    class ORMPagedIterator implements IteratorAggregate {
        function __construct($orm, $filter, $sorting, $chunksize = 100) {
            $this->orm = $orm;
            $this->filter = $filter;
            $this->sorting = $sorting;
            $this->chunksize = $chunksize;
        }
    
        function iteratorNext($i) {
            $offset = $this->chunksize * $i;
            $recs = $this->orm->select($this->filter, $this->sorting, $this->chunksize, $offset);
            if ($recs) {
                return new ArrayIterator($recs);
            }
            return null; // end reached
        }
    
        function getIterator() {
            return new NextIteratorCallbackIterator(array($this,"iteratorNext"));
        }
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are many questions and answers about this, but I am looking
There are many free online Source Control available but I would like to know
Friends, I know how to deploy and retrieve a single element in LINQ, but
I know im missing something but my friend and I can figure out what.
I am using twitter4J to get a user's friends timeline, but it is not
Hi i just wanted to know, is there any way to know how many
There seem to be many many unanswered questions about this. Hopefully, I'll get lucky
Hello Friends I want to know is there any way we can open or
Friends, I like to know using which version of Android SDK we can develop
Friends, If I want to know about the new features of Oracle 11gR2 Database

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.