So I’ve just finished version 0.4 of a module for handling user feedback for CMS Made Simple. It allows users with administrator rights to create reasonably complex forms, with all the user interface objects that we’ve come to expect, and handle the submission of those forms in a variety of ways.
It’s been an interesting experience. Version 0.1 was your standard naive PHP implementation of an application within a framework. It was all one big script in one big file. It would do fairly simple database queries, stuff all the results into a big array, and then process array elements with big switch statements when it needed to customize output based upon UI object type.
Version 0.2 ported this basic functionality into an Object Oriented model. I found that there were a couple of complex decisions to make — should I handle the database storage and retrieval in an OO manner? And if I query a bunch of Input Objects from the database, how do I know what specific kind of object to instantiate since that data is contained in the database record? I guess the truly OO approach would be to use an OO database, or, next best approach, to have a separate table in the database for each kind of input object. But that’s not how I did it, probably to the detriment of my code (I instantiate a superclass object, then use the type details from the general object to create a new object which is the correct specific class.)
Still, this showed how a procedural approach could save a lot of database activity over an OO approach. The data model comprises forms, which contain one or more fields, which have one or more options. In the procedural approach, I denormalize the database so that fields contain the form id, and field options contain both the field id and the form id. I could then grab everything I needed for a form in three queries. With the OO model, the number of queries is proportional to the number of fields. What’s more, there’s been a massive proliferation in the number of files required. While I worry about the web server having to load and parse all that stuff each time, I should probably have more faith in the PHP engine and the OS caching. As a number of people have said to me, I’m not playing on a TRS-80 with 4k RAM anymore. But I still feel like I should be programming as if resources were seriously limited.
Similarly, when it came time (for version 0.4) to add localization of the code, it required some somewhat unpleasant contortions: each Input object needs to have access to a global collection of text strings (stored in a big hash) so they can present localized versions of messages. And I still need to go in and make sure that I’m actually handing around references rather than the copies that PHP likes to pass.
Maybe this is just the yammerings of someone who should understand software design better. Clearly, there were some bad decisions made in the code, although I could argue about how bad they really are.
Another aspect that took me by surprise was how I could test code and have it perform perfectly, while other users reported errors and bugs immediately. In this case, the main culprit was not inattentiveness (in testing, anyway), but my PHP configuration. If you allow output buffering, PHP gracefully handles output before the headers have been sent. Not so, if output buffering is disabled. So when my code would generate errors, my test configuration would blithely allow error codes to be output but then clobber that output with the expected page output. So while I thought output buffering was only involved in performance, it seems that for development, it should be disabled. That way, those bugs cannot be so easily overlooked.