在对象中调用Object


PHP - Calling Object in an object

我有一个用MYSQL和PHP创建OODB的类项目。

目前,我有一个充满对象框的表。我还有一个box类,当它被构造时,它将从表中提取数据,然后以类似的方式递归地构造它的子类。这似乎很有效。但是我无法从子框调用函数。

下面是类:

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=$row[2];
              $this->type=$row[3];
              $this->siblingorder=$row[5];
              $this->content=$row[6];
              //echo $this->id."<br />";
              if(isset($this->children)){
                 //echo $this->children."<br />";
                 $kids = explode(',', $this->children);
                 foreach ($kids as $key => $value) {
                     $varname = "box".$value;
                     //global $$varname;
                     //echo $varname."<br>";
                     $$varname = new Box1($value);
                 }
             }
         }
    }//constructor
    function display(){
         echo "<div style='border: solid 2px;'>";
         echo $this->id;
         echo "<br />";
         if(isset($this->children)){
            $kids = explode(',', $this->children);
        foreach ($kids as $key => $value) {
                $varname = "box".$value;
                //echo $varname."<br />";
                $$varname->display();
        }
         }
         echo "</div>";
    }//End DISPLAY
    function update(){
    }//End UPDATE
}
下面是调用构造函数和显示函数的代码,而显示函数又应该调用子框的显示函数:
    $box1 = new Box1(1);
    $box1->display();

如第一条注释所述,问题在于$$varname是在构造函数内部创建和赋值的。但它不存在于函数显示中。曾经的建设者被调用时,这些变量不再存在。下面的代码向您展示了如何使子对象成为类型为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
}

[*]:这里我们决定children总是一个Box1的数组。当然,如果没有子数组,这个数组可以为空。这是一个品味问题,有些人宁愿让它无效,如果没有孩子。但是在这种情况下,您必须在遍历display()中的$this->children之前检查null值。