I'm trying to do some recursive work with classes. And I run in to problems
executing the recursive actions outside the constructor.
I've narrowed it down to this, its simply returning levelnumbers as of know,
but the problem still exists.

this does not work, it only goes to level 2, where is should go to level 5:

class Recurse {
 var $level;
 var $children;

 function Recurse($ParentLevel = 0){
  $this->children = array();
  $this->level = $ParentLevel+1;
  //$this->parse();
 }

 function parse(){
  if ($this->level < 5){
   for ($i = 0; $i<= $this->level;$i++){
    array_push( $this->children, new Recurse( $this->level ) );
   }
  }
 }

 function getHtml(){
  $text = "$this->level<br><ul>";
  foreach ($this->children as $child){
   if (is_object($child)) $text .= $child->getHtml();
  }
  $text .= "</ul>";

  return $text;
 }

}

$n = new Recurse();
$n->parse();
echo $n->getHtml();

However. If i do
$this->parse();
within the constructor function, it works like a charm. (remember to
outcomment $n->parse();)
This is really not what I want to do in my real class.
Is there any way to fix this without moving the parse execution within the
contructor?

/Christoffer
PHP Version 4.2.3

Reply via email to