Extending DOM objects

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Extending DOM objects

Post by Ambush Commander »

When working with the DOM extension, I've often wished that I could extend the default class behavior to add some convenience methods. Alas, this does not seem to be possible without runkit or editing the extension source, but using the Visitor design pattern this can be somewhat alleviated (the syntax is still clunky, but the general behavior stands).

What I'm here to ask about is assigning custom member variables. When I do this:

Code: Select all

$dom = new DOMDocument();
$dom->silly = 'foobar';
print_r($dom);
I get:

Code: Select all

DOMDocument Object
(
    [silly] => foobar
)
So it appears the property is preserved. Two questions:

1. Is this a good idea? (I'm thinking of adding line number information and other data to nodes, which is why I need this)
2. Why don't any of DOMDocument's other, built-in properties show up when I print_r?
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

What do you mean you can't extend DOMDocument? Did you try to because it sure works on my server.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Oh, I can extend it all right, but I can't get it to create extended nodes, etc without overloading all of its methods too, which I have no interest in doing.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Ambush Commander wrote:Oh, I can extend it all right, but I can't get it to create extended nodes, etc without overloading all of its methods too, which I have no interest in doing.
http://us.php.net/manual/en/function.do ... eclass.php
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Ooh, that's really great! But I'd still like to know what your thoughts on my method are: it's seems that registerNodeClass is only available in PHP 5.2 or later.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

I am not 100% clear on why you are doing that, but I am thinking its probably not a great idea. Look into your soul, if it feels hackish or messy, or if someone else reading the code is going to go 'wtf?', then its probably not a good idea.

I would suggest biting the bullet and overriding all the methods (there really aren't that many), or seeing if you can get up to php 5.2.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It's for a library, so I want all the compatibility I can get. It does feel hackish, but there will only be a few extra member variables and I'll remember to document them (somewhere... no explicit definition).

Which brings up some other difficulties: can I rely on DOM being available in PHP 5? It can be disabled, so I'm a bit worried about that.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

Its built into PHP and not an external library, so I think you are pretty safe, just make sure that you note in your requirements that it depends on DOMDocument. Its possible to disable any function or class in php if someone really wanted to.

You could also do a check in the include and print out a friendly notice asking them to turn on DOMDocument.
ev0l
Forum Commoner
Posts: 56
Joined: Thu Jun 21, 2007 1:50 pm

Post by ev0l »

As I see it you have 4 options.
  • 1. Just subclass what ever classes in which you need to change the behavior and override the needed methods.

    2. Modify the PHP source (the C source) to do what you need.

    3. Write your own DOM library.

    4. Find another DOM library that does what you need or you think it easier to modify
1 looks the most attractive. I would suggest you look at the implementation of the DOM classes in the PHP source. The PHP source is quite attractive and rather easy to follow. In some cases it is must more enlightening than the online documentation.

You may find that due to some unexpected reasons, like the overuse of private (I really dislike private), it might be easier to modify the PHP source.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I've been down this dusty trail, and using registerNodeClass was the best way to go in the end.

That being said, I ditched it in a heartbeat for ActiveRecord. Maybe an ORM solution would be a better fit?

Also check out Maugrim's Data Objects article and see if that inspires you: viewtopic.php?t=48499
Post Reply