简体   繁体   English

PHP-在对象中调用对象

[英]PHP - Calling Object in an object

I have a class project creating an OODB with MYSQL and PHP. 我有一个课堂项目,使用MYSQL和PHP创建OODB。

Currently I have table filled with the object boxs. 目前,我的桌子上装满了对象框。 I also have a box class which when it is constructed will pull it data from the table and then recursively construct its children in a like manner. 我还有一个box类,在构造它时会从表中提取数据,然后以类似的方式递归构造其子级。 That seems to be working well. 这似乎运作良好。 But I am unable to call a function from a child box. 但是我无法从子框调用函数。

Here is the class: 这是课程:

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

}

Here is the code calling the constructor and the display function which in turn should call the children boxes display function: 这是调用构造函数和显示函数的代码,它们又应调用子框显示函数:

    $box1 = new Box1(1);
    $box1->display();

Any help or insight would be much appreciated. 任何帮助或见解将不胜感激。

As stated in the first comment, the problem is because $$varname is created and assigned inside the constructor. 如第一条评论所述,问题是因为在构造函数内部创建并分配了$ varname。 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 在下面找到一些代码,该代码向您展示如何使子代成为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. [*]:这里我们决定子代始终是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(). 但是在这种情况下,您必须在遍历display()中的$ this-> children之前检查null值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM