Thought I might make some notes about this to help the next person along.

Firstly, PHP5 on windows just has the XML SAX parser installed by default so, even though there is a PEAR XML library, you don’t need it.

Attributes

The Java classes have a org.xml.sax.Attributes class. When you get passed this you can call a method to get values you are expecting, e.g.:

1
2
3
4
5
6
7
8
  void handleTable( final Attributes att )
  {
    firstColumn = true ;
    currentTable = att.getValue("name");
    os.println("create table " + currentTable );
    // Set up markers to create the tags table later
    if ( "true".equals(att.getValue("hasTag") )) tagTables.put(currentTable,currentTable) ;
  }

In Java there is an idiom where if there isn’t a value it will return null (which is why I have the string to test against the constant string first in the equals test, so that you don’t get a null pointer error if the attribute is null). PHP, on the other hand, uses the very useful associative arrays and the function became:

1
2
3
4
5
6
    function handleDomain( $att )
    {
      os::doprint("create domain " . $att["name"] . " " . $att["type"]);
      if ( array_key_exists( "nullable", $att ) && $att["nullable"] == "" ) os::doprintln(" not null");
      os::doprintln(";");
    }

Notice that I now have to use array_key_exists to test for something’s existence before I can do anything with it.
In this class, instead of having a local variable that is set to a print stream I have created a static class that I can use to do the printing instead. I may change this again, not sure.

A lot of the O-O junk isn’t there in PHP, a class is static if it doesn’t have any methods that need to use the this variable. Purists won’t like this but I don’t care, to be honest.

Method calls

One thing to be aware of is that PHP insists on the $this→; operator in front of any class methods. If you don’t put it there it will think you are calling an internal function but won’t complain until you try and call it.

Simplification using associative arrays

This:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void handleTags()
  {
    String table = null
    ,      column = null ;
    Object [] theKeys = tagTables.keySet().toArray();
    for (int i = 0; i < theKeys.length; i++) 
    {
      table = (String)theKeys[i] ;
      column = (String)tagTables.get(theKeys[i]) ;
      os.println("create table " + table + "_tag" );
      os.println("( " + column + " id references " + table + "(" + column + ") not null" ) ;
      os.println(", tag_id id references tag(tag_id) not null" );
      os.println(");");
    }
  }

Becomes:

1
2
3
4
5
6
7
8
9
10
    function handleTags()
    {
      foreach ($this->;tagTables as $table =>; $column) 
      {
        os::doprintln("create table " . $table . "_tag" );
        os::doprintln("( " . $column . " id references " . $table . "(" . $column . ") not null" ) ;
        os::doprintln(", tag_id id references tag(tag_id) not null" );
        os::doprintln(");");
      }
    }

Which I think is quite good. A ton less code to get to what you want to do. I think there is a similar thing to foreach in java 1.5 but nothing as easy to use as this.

All in all, for getting the job done, I think PHP wins hands down. Java is very low level and has to do everything through a class and chuck arrays of Object around. The people who developed PHP weren’t afraid of putting things like associative arrays in the core language and this saves a whole ton of code. If you want to override methods and go the whole O-O hog you can, but you have to explicitly say you are going to do so.

PHP5 has most of the Java-like constructs apart from this see http://www.developer.com/lang/php/article.php/10941_3302171_1