views:

26

answers:

0

I am trying to write and implement a simple node/tree interface in PHP. Something along these lines:

interface node
{
    public function setParent(node $node); // can be null
    public function removeChild(node $node); // should not be null
    public function addChild(node $node); // should not be null
}

The implementation details of each node must be hidden from other nodes. In order to perform atomic operations relating two nodes, they will have to call each other in a re-entrant fashion.

An implementation of setParent might be:

class myNode implements node
{
    protected $parent;

    public function setParent(node $node)
    {
       if($this->parent !== $node)
       {
           if($this->parent) $this->parent->removeChild($this);
           $this->parent = $node;
           if($node) $node->addChild($this);
       }
    }

    // ... snip ...

}

Is this a proper way to express this idea?