Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple tree class

Status
Not open for further replies.

p3t3

Programmer
Aug 21, 2002
46
0
0
GB
Hi, I have a simple tree class,

class mTree {

var $value = null;
var $children = array();

function mTree ($value) {

$this->value = $value;
}

function addChild ($value) {
$aux_node = & new mTree ($value);
$this->children [] =& $aux_node;
$this->displayTree(".");
return $aux_node;
}
function getName (){
return $this->value;
}

function displayTree($indent){

echo($indent.$this->value.&quot;<br>&quot;);
echo(&quot;children&quot;.count($this->children).&quot;<br>&quot;);
forEach ($this->children as $child){
$child->displayTree($indent.&quot;.&quot;);
}
}
}

------------------------------------------------

I add a couple of test nodes thus,

$dirRoot = new Mtree(&quot;PROJECTS&quot;);
$newNode = $dirRoot->addChild(&quot;test1&quot;);
$newNode->addChild(&quot;test1_1&quot;);

my problem is, after adding &quot;test1_1&quot; the debuging code displays the tree from &quot;test1&quot; fine (i.e one child), but if I display the tree from $dirRoot, only &quot;test1&quot; seems to exist (i.e the child node vanishes). I assume I'm doing something stupid with references but Im lost...

Pete...



Digital Soma
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top