As stated in the first comment, the problem is because $$varname is created and assigned inside the constructor. But it does not exist in the function display. Once the contructor has been called, these variables do not exist anymore. Find some code below that shows you how to make children an array of objects of type Box1
class Box1 {
var $id;
var $pid;
var $children;
var $type;
var $content;
var $siblingorder;
function Box1($bId){
$q ="SELECT * FROM `box` WHERE id =". $bId;
$r = mysql_query($q);
if ($r){
$row = mysql_fetch_array($r);
$this->id=$bId;
$this->pid=$row[1];
$this->children = array();//[*]
$this->type=$row[3];
$this->siblingorder=$row[5];
$this->content=$row[6];
//now we fill this->children with objects of type Box1
if ($row[2] != '') {
$kids = explode(',', $row[2]);
foreach ($kids as $value) {
$this->children[] = new Box1($value);
}
}
}
}//constructor
function display(){
echo "<div style='border: solid 2px;'>";
echo $this->id;
echo "<br />";
foreach ($this->chidren as $kid) {
$kid->display();
}
echo "</div>";
}//End DISPLAY
function update(){
}//End UPDATE
}
[*] : Here we decide that children is always an array of Box1. Of course this array can be empty if there are no children. It's a matter of taste, some people would rather let it to null if no children. But in this case, you would have to check for null value before you iterate over $this->children in display().