Rob's Ramblings

Monday, 7 February 2011

php classes are go!

It's a bit of a mind set switch. Most of my time spent employed as a programmer was writing business applications in BOS/COBOL. This mostly involved leading the user through a series of options, with little chance of random things happening. The inclusion of SpeedBase added a little more OO style coding - it's basically a windows manager and relational database spliced on top, so you end up writing snippets of code for the different actions a user could do or buttons they could press within a window, but the options for them were still fairly limited, but that's probably what you need when dealing with Ledgers and Order Processing systems - make things too complicated and you have more chances to introduce errors or, god forbid, bugs!

Obviously this approach doesn't lend itself to programming for the internet, where as far as the server is concerned, pretty much any request could come at any time for any thing! And with forms, you don't get to prompt a user each item in turn, you basically get the whole set of answers dumped on you all at once. You also can't rely on the answers actually being within the range of values you specified when you displayed the form, necessitating a fair bit of validation being required to stop people trying to disrupt your systems. So it was fun getting used to such changes when I started coding up web backends.

Now, I'm working, finally, on converting my Viewdata Viewer code to a class based system. At present, it's a loose collection of individual programs that can each operate on a range of data formats, but each programme does different things, on different sorts of data, in different and sometimes contradictory ways. It started of as just a single bit of code that allowed me to refer to a saved viewdata page within an <img> tag, rather than have to convert it to an image manually, but it snowballed, especially with the inclusion of over 11 file formats!

The conversion is to try and create a defined API between the data files and the programs that need to access them. A lot of the code can obviously be re-used, but I'm trying to clean some bits up as well. Out goes the spaghetti of ifs and elses, and in come switches. Out go hard coded constants, and in come some defined labels. It should make accessing the files much easier, and will hopefully allow for such things as:

$vfile = new ViewdataViewer();
echo $vfile->ReturnText();

Which is obviously much simpler than trying to figure out the file format, etc, in every app.

The main intended application at the moment is for a database for the Celebrating Viewdata site, into which all the pages found so far can be loaded, with an intent to create functionality similar to Obviously items in the database need to be stored in a consistent format, and therefore I needed code to parse the source data files. Given I would have to replicate the majority of the functionality currently in place in vv.php, vl.php and vb.php, I thought it best if I bit the bullet and did it right!

Having all the file-format-dependant stuff in a single class file will also, of course, enable the addition of any new file formats to be achieved with the minimum of fuss, and no editing needed to existing applications. It should also make it much, much, easier for anybody else who wants to use the code to actually use it!

Of course, classes are a totally new way of thinking about items, again. It took a little playing and reading before I got the hang of them. It's a little more involved than just writing a library of subroutines, but much better. I wish I'd known about them before I started the viewer originally. But, well, it did start out as barely more than a hundred lines of code that achieved but a single task. It grew somewhat from there...

Anyway, watch out on the project website for the class code to appear. Hopefully I'll be able to post the first working code up within a week or two. Status as of today is that the recognition seems to work, and some of the support routines are coded up for some of the filetypes! I'm pleased with it, and enjoying the coding, which always makes it go faster. I just have to find the time to actually do it..

Labels: ,