Hi!
Well, I share your confusion :-)
Actually, I obviously couldn't upload a working example because it's bigger than this. When working on a few projects, I realized I essentially wasted time on coding data handling routines. Like: Get row from database, add to results, get next row.
When I had to redo an array-based approach (I used to have a dynamic menu which first needed to be small, so I hard-coded an array into the source, but then it had to grow, so I had to use a database), I decided to rethink the whole approach and standardize it a little.
If I extend the first on the second, I lose this functionality:
1) Just as I exemplified above, if I use an array of type array(array(), array()) at first and then go to a database, I will have to make all the printer objects extend another datahandler object, which is a bit stupid since I may still need to handle the raw information interchangeably with either the array-based datahandler or the database-based (perhaps using the phpLib database class or Pear's db class - all of the return a row as an associative array, which is exactly what I need). Then, what also happens is that I also need to change the getCurrent and getNext methods of all (!!!) printers that switched from array to database.
Not very nice, really.
There's a bunch of other things which are whirling around in my head but which I can't think of right now, but having a datahandler and a datarender classes is one of the top five in books on OOP software architectures.
Having a separate printer is also good because I may switch to another data-handling algorithm which will be generic and won't need to create a separate printer for each case.
For instance, we pay pass a row template to the printer (i.e. <tr><td>{firstname} {lastname}</td></tr>😉. The printer will replace each macro in the template with the value of the respective key in its data array. This will allow us to make it a very generic and reusable class. But then, if for some reason you will need to switch back to just inserting values into raw HTML, it won't be as difficult as with the standard approached (I mean just plain PHP functions).
The printer could, for example, incapsulate a keycol variable that allows us to print only the unique rows within a non-unique result set. This is also very effective.
I know I am losing a couple of microseconds on processing this way, but I see this as a better way to design software in PHP.
Thanks a lot, I would appreciate your further comments!
Best,
Stas