As most of you who read this know I’ve been writing a system to do the data entry for my collaboration system. It had the following objectives:

  1. The data model (and as far as possible the boiler plate etc.) is in an XML document.
  2. This XML document is used to
    1. Create the schema
    2. Render HTML forms
    3. Generate the insert/update SQL from a structured object
    4. Define navigation paths between tables (peer, master/detail)
    5. Define lookups and foreign keys
  3. The resulting system should be database agnostic.

I originally started in Java and wrote the code that generates the schema from an XML document probably 18 months ago. I then looked at things like Jackarta Struts and other Java technology to deliver the data entry and the rest of the application, because I knew that Oracle on its own was a non-runner and I didn’t want to pay a small fortune in licences to them for a system that may sink without trace.

Problems: productivity with Java is crap. Struts was too hard to get things going in. I could have written it all as a pile of JSP’s but it would have been a pig to maintain. I have learned a lot from learning Java (and getting Sun Certified) but it’s poor for small projects.

I then decided (about 2 months ago) to look at using PHP; in fact a friend suggested it to me. PHP differs from Java in that there are a lot of very useful things in the base language that you don’t have to redefine for yourself. Also it uses associative arrays. Associative arrays alone can save you a great deal of time if you know how to use them properly and the language has proper syntactic sugar to manage them. I already knew this from using Awk. These exist in Java, but they are yet another class you have to learn, not part of the language. Java 1.5 does have some syntax to walk these kind of objects but its too little too late.

Java was created by computer scientists for computer-sciency reasons. PHP is extremely pragmatic. PHP 5 stole the good bits out of the C++ and Java object models but made them optional. In Java everything is an object (apart from the base types which have a ton of irritating rules around them). In PHP a variable is a variable and its type can change.

In the 8 or so weeks, putting in about 6-10 hours a week I have managed to get to a place where I can insert data and render a data entry form in HTML just by getting information from my structured object.

Part 1

I have already described how easy it was to change the original XML parsing code that created the schema into PHP from Java. This took me about 3 days, and at least part of this was learning the syntax of PHP as I went.

Part 2

This is where the giants come in. My HTML forms are rendered by the Pear package HTML_Quickform, the database management by the DB package.

As an example, I was worrying about how I was going to do sequences for ID’s etc. in my insert forms. No problem. The DB package has a generic way of generating them and does its own thing with the underlying database. I just cut and paste the examples out of the manual and change some variable names, it works.

Another example – I was constructing dynamic SQL to take the list of names and values in the associative array used by the quick form object. I didn’t need to. The DB package will do all of the donkey work for you if you pass it the table name with an array of column names and values, and tell it what you want to do. I did have to do things like remove the elements from the values array that were control data, and also (for the update) create a where clause. But it was trivially easy.

Part 3

You don’t need a complex IDE full of demented wizards. You can just sit in a directory and work on your code. Vim and Apache are all that is required as long as you are disciplined. I recon my productivity is maybe 10 times what it would be in a Java IDE. No compiling. Hit refresh on your browser and you can see your changes straight away. No constantly restarting your J2EE containers – there aren’t any!!! It works really well with the make a small change test the results way of working, without all of the constant pauses while the IDE thinks about things.

I am beginning to think that even a Java shop could benefit from PHP if you use its object model. This is because it should be fairly easy to translate the PHP to Java after you’ve resolved the issues around what you want the application to look like. You could even write a first-cut translator in PHP (or better Python – which is brilliant at text manipulation). Just treat Java as what it is; the lowest common denominator, the C of the 21st Century.

Gotta get some sleep.