Quantcast
Viewing all articles
Browse latest Browse all 4

Treat a PHP class that implements Iterator as an array

If I have a class that implements the Iterator interface, I can manually control how iteration in a foreach loop. But are there other ways in which I could make my object behave like an array?

For instance, let's say I have a class Guestbook which implements Iterator, so that I can iterate foreach (new Guestbook() as $entry). But what if I want to, say, reverse the order?

foreach (array_reverse(new Guestbook()) as $entry) definitely won't work, because array_reverse will only accept an array.

I guess what I'm asking is, can I use Iterator for more than just foreach loops?

Thanks.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>