Page 1 of 1
Extending DOM objects
Posted: Wed Oct 03, 2007 6:32 pm
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?
Posted: Wed Oct 03, 2007 7:05 pm
by Begby
What do you mean you can't extend DOMDocument? Did you try to because it sure works on my server.
Posted: Wed Oct 03, 2007 7:07 pm
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.
Posted: Wed Oct 03, 2007 7:22 pm
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
Posted: Wed Oct 03, 2007 7:24 pm
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.
Posted: Wed Oct 03, 2007 7:41 pm
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.
Posted: Wed Oct 03, 2007 8:11 pm
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.
Posted: Wed Oct 03, 2007 8:52 pm
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.
Posted: Thu Oct 04, 2007 10:33 am
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.
Posted: Thu Oct 04, 2007 2:35 pm
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