CityStats: XML parsing in PHP
Started work on figuring out how to use the DOM implementation in PHP to parse the complex query data coming in from the wizard. These are some gotchas that slowly emerged as I struggled with this stuff, which I haven't really used before:
The DOM implementation is a bit flaky. For instance, it appears that when you get a DomNodeList
, and iterate through it with foreach
, if you make changes to the elements in the list within the loop, you sometimes invalidate the pointers in the list. Also, methods which return an object such as a DOMElement
can't be reliably used unless you assign the result to a variable; if you call a method on the return of the method directly, it often fails.
Also, PHP (it seems) has problems with using DOM objects passed by reference as function parameters. I was using a practice I've often used in better languages such as Delphi, where I pass references to a DOMDocument
and a DOMElement
into a method of another object, so that the object can use them to create new elements and add them as children of the argument element, but it turns out that while I'm able to use reader methods of these objects, whenever I try to do anything which modifies the document or the element, the script just blows up (no errors, just blank returns). I suspect this might be due to some sort of scoping error; perhaps PHP is tracking the "modified" status of the DOMDocument using an external variable of some kind, tucked away somewhere, and this variable is not accessible from my object's method, so trying to alter the document structure results in an attempt to access that variable when it's not accessible. This is just a guess, though. Suffice it to say that using DOM methods in PHP is pretty time-consuming and frustrating.
I got two classes written and tested, and in the process figured out what most of the limitations are, so I should be able to move forward faster from here. Writing good OO code in this language is hard, though. And the Unicode support is completely missing. That's amazing in 2008.