【问题标题】:PHP - Calling Object in an objectPHP - 在对象中调用对象
【发布时间】:2013-05-07 17:16:45
【问题描述】:

我有一个使用 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();

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

【问题讨论】:

  • 这是变量范围的问题。 display() 无法访问 $box1,因为它不在该函数的范围内。我可能会创建一个子对象数组作为对象的属性。

标签: php mysql oodb


【解决方案1】:

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

【讨论】:

  • 经过一些非常小的调整,例如将 l 添加到 ->chidren 之后,它起作用了。非常感谢。
猜你喜欢
  • 2014-10-08
  • 2021-12-24
  • 2010-12-11
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
  • 1970-01-01
相关资源
最近更新 更多