Search code examples
phpforeacharrayobject

Let an object loop as array with a foreach() by extending ArrayObject


So I have an object which contains a set of objects in a private data member. I can now loop it with a for loop by overriding the count() function of an ArrayObject and offsetGet($index), but I also want to loop it within a foreach loop.

What functions do I need to minimally extend to add this functionality?


Solution

  • Use the SPL Iterator Interface see http://uk.php.net/manual/en/class.iterator.php for details