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

  • Home
  • SEARCH
  • 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 8883151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:44:47+00:00 2026-06-14T20:44:47+00:00

For the sake of brevity, I’ll simplify my example; say I have an index.php

  • 0

For the sake of brevity, I’ll simplify my example; say I have an index.php file in which I instantiate the repository class like this:

$itemRepos = new ItemRepository();
$allItems = $itemRepos->getAll();

and at this point in the variable $allItems I have all the records returned to me from the database, and now I can iterate through them.

But now, I would need to (because of the memory issues) read the items from the database one by one, and surely, in the index.php file I could make a connection to the database and use the while loop like this for example:

while ($row = mysql_fetch_row($result)){
    //do stuff
}

and thus reading the rows one by one, but I don’t want to do it like that and I was wondering how do I achieve the same thing by using the OOP principles, so without putting the db connection logic into the index.php file, and by (somehow) putting this in the Repository class.

I do think some kind of pattern or ‘something’ already exists for this, so please do push me in the right direction.

  • 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-06-14T20:44:49+00:00Added an answer on June 14, 2026 at 8:44 pm

    When you don’t want to expose implementation details but still want to be able to iterate, you should use the Iterator pattern. The purpose is to provide a unified way that clients can iterate over something, while keeping the underlying implementation encapsulated.

    PDO offers an iterator over result sets without you having to code anything. But, if you must stay with the mysql_* extension for now, you can write your own iterator by implementing Iterator.

    You might actually find some example code in the manual for making an iterator to encapsulate a mysql_* result set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following base and derived (partial, for sake of brevity) classes: class
For brevity's sake in my code, i'd like to be able to do the
For simplicities sake, I'll make up a similar example to what I have: Let's
First some context: I have an MVC3 .net project which, for the sake of
I have four entities Organization and Address ..for the sake of brevity I have
I have following code (only relevant portions shown for sake of brevity - please
I have a page which essentially looks like this: <div id=foo> <a>One</a>, <a>Two</a>, <a>Three</a>,
For the sake of the example, consider a table create table foo ( contents
For the sake of education, and programming practice, I'd like to write a simple
For the sake of argument, here's a simple person class public class Person :

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.