Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Note to self

When writing an XML parsing class in PHP, don’t forget the ampersands in the following code snippet:

xml_set_element_handler($parser, array(&$this, '_tagOpen'), array(&$this, '_tagClose'));
xml_set_character_data_handler($parser, array(&$this, '_cdata'));

Failure to include them can lead to two days of debugging, frustration and hair loss. Solution found thanks to Keith’s PHP rant (which itself reads like the result of several days of hell). Roll on PHP 5, where objects are passed by reference by default.

This is Note to self by Simon Willison, posted on 13th August 2003.

View blog reactions

Next: PHP Library Tips

Previous: Python never copies implicitly

7 comments

  1. Or use xml_set_object() which exists precisely for this reason.

    Chris Adams - 14th August 2003 01:40 - #

  2. This would fail if allow_call_time_pass_reference was off, wouldn't it?

    anode - 14th August 2003 02:18 - #

  3. I've all but avoided PHP classes outright because of the reference issues. Maybe I'll end up trusting them in PHP5, but I'm already too used to procedural design with PHP. Habits shall die hard...

    Patrick Lioi - 14th August 2003 02:22 - #

  4. I'm sure this will come off sounding snotty, but I can't fathom why this is so difficult for people. The whole problem seems to arise from people expecting PHP to act like other languages, and I don't understand that expectation. The PHP developers never said "We're going to make this part of PHP behave like Language X", so there's no reason we should expect otherwise. For good or ill, I expect PHP to behave like PHP, just as I expect Java to behave like Java and Python to behave like Python. A couple years ago, a line of code I'd written screwed up because I didn't tell PHP to pass a particular object by reference. So I looked in the documentation, read the part where it quite plainly said "you must tell PHP when to pass by reference", and I never had the problem again, ever, because I understand how PHP behaves and don't expect it to behave otherwise. PHP5 will behave differently than PHP4, but that doesn't mean I'm thinking to myself, "What a relief, now PHP behaves like Language X!"

    Jordan - 14th August 2003 05:21 - #

  5. Jordan: I think the complaint isn't so much that "PHP doesn't behave like Language X", but that "PHP would be more elegant if it behaved like Elegant Language X, and Elegant Language X has said feature for a good reason."

    Patrick Lioi - 14th August 2003 19:24 - #

  6. You're correct, of course, Patrick. I intended to mention something of this sort in my post, but forgot by the end of my rant. PHP would definitely be more elegant (or, more simply put, easier to use) if it behaved like other languages in this respect. Which is, of course, why they're changing it for PHP5. I just don't understand where this hair-pulling frustration comes from -- once you've learned how PHP behaves, for good or ill, you've learned it and you'll remember it, and there's no reason it should trip you up more than once, ever (that is, unless you go away from PHP for a long while and then come back).

    Jordan - 14th August 2003 19:50 - #

  7. YALL ARE BOTH WRONG!!! C++ all the way!

    KevinMcPants - 23rd October 2003 04:33 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2003/08/13/noteToSelf

A django site