I am currently working on fixing a bug (in my software) that i tracked down to what i think is a singleton in my collection. Every time i loop over the collection i reset the position to 0 , so as far as i understand every loop that is running the same object will be affected.
class Collection implements \Iterator {..}
$coll = new Collection();
foreach ($coll as $a) { // loop A
foreach($coll as $b) { // loop B
// triggers rewind and affects loopA
}
}
i am pretty sure i am missing something big or maybe i am trying something that should be solved differently? should i create a copy for every loop do? but i do want the object to share the same data but just not the position? should i create a unique key for every rewind and store different position for each in an array of positions? (sounds doable but .. wrong? there must be a better way.. right?)
i found this 12y thread that describes the same problem but i hope something has changed in that time? Do people just dont do it and use arrays instead? have other languages the same problem?
nested foreach with iterator interface