Originally posted by poison
thanks for your answer =)
now things are getting really strange...
may I ask you what PHP version you are running ?
I've tested with 4.3.2 and 4.3.5, and it doesn't work....
Oh, good point; I'm using 5.0.0RC1 at the moment. Hang on a tick; I'll install - ah, 4.3.0 - and try that.
Okay. I see. $this->parent is always ending up null. setParentNode() is working (parents can be added by hand á là $t2->_setParentNode($t1);), so it must be add() that's choking.
It is recognising that those $data which are Nodes are in fact Nodes (and wouldn't it be better to distinguish between nodes and data?) and they are being set with their parents there, but they're not surviving beyond that.
Ah, but $data is not being passed by reference but by value - it's a copy, and $this->_elements[$index] is being made a reference to that copy, not the original. It's the copy that's having its parent set, not the original - whose parent remains null.
So the parameter passed to ->add() has to be by reference; i.e., declared as function add(&$data) so that modifications to $data work on the original object and not a copy; that's why it works in PHP5 - objects are automatically passed by reference.
Of course doing this means that $foo->add('forty-two'); won't work, because PHP4 needs the argument to ->add() to be something it can make a reference to. It means either $forty_two = 'forty-two'; $foo->add($forty_two); or separating the tasks of "adding children" from "adding data".