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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:58:25+00:00 2026-05-10T20:58:25+00:00

I have a class and I want to be able to iterate over a

  • 0

I have a class and I want to be able to iterate over a certain array member. I did a quick search and found IteratorAggregate:

class foo implements IteratorAggregate {     protected $_array = array('foo'=>'bar', 'baz'=>'quux');      public function getIterator()     {         return new ArrayIterator($this->_array);     } } 

which works great, but doesn’t that create a new ArrayIterator instance every time foreach is used on it?

So I thought I should store the iterator instance in a member:

    protected $_iterator;      public function getIterator()     {         if (!$this->_iterator instanceof ArrayIterator) {             $this->_iterator = new ArrayIterator($this->_array);         }         return $this->_iterator;     } 

The problem is that the iterator uses a copy of $this->_array during the first call of getIterator(), so changes to the member aren’t reflected on subsequent foreach constructs.

I was thinking I should subclass ArrayIterator, add a setArray($array) method and call it before returning it in getIterator(), but I don’t know the member name of the array it uses internally and whether or not it’s overwriteable by a subclass.

The question is: is this a premature and/or unnecessary optimization? If no, what’s the best way to achieve this?

  • 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. 2026-05-10T20:58:26+00:00Added an answer on May 10, 2026 at 8:58 pm

    You second code will fail on multiple loops of the object where you start a new loop before another is finished.

    Didnt really look like an optimization worth spending time on either.

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

Sidebar

Related Questions

I have a C++ class MyObject and I want to be able to feed
I have a class that I want to be able the handle the mouse
I have class and I want to initalize column that stores pictuer with type
I have a class that I want to contain multiple objects of something I
I have a class and I want to have some bit masks with values
I have a class that I want to include in a GWT module. Unfortunately,
I have a class which I want to add a property with using formula
I have a C++ class I want to inspect. So, I would like to
I have a python class I want to instantiate and the __init__ definition has
I have this class which I want to pass around Windows as LPARAM parameter.

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.