Implementing PHP Iterators - The Beginning

One of the new features that came with PHP 5 is the ability to implement Iterators. Implementing one of your objects as an iterator essentially allows you to treat it as a standard array. By default, without adding any additional code, you can enable iteration over a regular objects variable members. So, for example, if I had the following code:

(I’ve removed the plugin that I used to format PHP code here, so until I find out that works, I apologize for not showing my code examples…)
If I were to run a foreach() loop over this, I would get behavior that would correspond to an array with the elements of $bar1, $bar2, & $bar3. This is wonderful default functionality. However, you can go a step further if you want to implement more complex & complete behavior. I’m going to be experimenting on this kind of behavior with my DBObject class - namely, converting it to a DBArray object, and see how well that goes over. I will report on my successes & travails in followup posts.

(Boy I’m having a hard time to get Wordpress to format code on this thing…probably need to get an extension to do it. Any suggestions?)

Basil Apr 7th 2006 09:48 am Development, PHP, Tutorials One Comment Trackback URI Comments RSS

One Response to “Implementing PHP Iterators - The Beginning”

  1. Sarfaraz Jamalon 09 Apr 2006 at 1:42 PM link comment

    there is a extension for displalying code

Leave a Reply

Creative Commons Attribution-ShareAlike 3.0 United States
Creative Commons Attribution-ShareAlike 3.0 United States