0

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

目前我的桌子上装满了对象框。我还有一个盒子类,当它被构造时,它会从表中提取数据,然后以类似的方式递归地构造它的子类。这似乎运作良好。但我无法从子框中调用函数。

这是课程:

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();

任何帮助或见解将不胜感激。

4

1 回答 1

0

如第一条评论所述,问题在于 $$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 之前检查空值。

于 2013-05-07T21:10:25.617 回答