Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对象字段不保存_Php - Fatal编程技术网

Php 对象字段不保存

Php 对象字段不保存,php,Php,我正在创建一个树,其中每个nodeParent对象都可以有子对象和子对象。在while循环中,我实例化一个nodeParent对象(因为我从文件中获取输入),然后使用一个堆栈添加它的子对象(子对象和子对象),以确定根。我在一个数组中添加了每个根父级,这样我就可以遍历树,但是当我试图访问它们的子父级名称时,我没有得到任何输出。 下面是NodeParent类: class NodeParent private $name; private $subParents=array();

我正在创建一个树,其中每个nodeParent对象都可以有子对象和子对象。在while循环中,我实例化一个nodeParent对象(因为我从文件中获取输入),然后使用一个堆栈添加它的子对象(子对象和子对象),以确定根。我在一个数组中添加了每个根父级,这样我就可以遍历树,但是当我试图访问它们的子父级名称时,我没有得到任何输出。 下面是NodeParent类:

class NodeParent
     private $name;
     private $subParents=array();
     private $children=array();

     function setName($name){
         $this->name=$name;
     }
     function addChild($child){
         $this->children = $child;
     }
     function addParent($parent){
         $this->subParents = $parent;
     }
     function getName(){
         return $this->name;
     }
     function getSubParents(){
         return $this->subParents;
     }
     function getChildren(){
         return $this->children;
     }
}
下面是我为每个NodeParent对象添加其子对象时的步骤:

$size = $stack->getSize();
            $subParent = new NodeParent();
            $subParent -> setName($name);
            $parent = $stack->peek();

            $parent -> addParent($subParent);

元素应添加到数组中,您当前正在使用
NodeParent
实例覆盖数组。可以通过以下方法推送到阵列:

class NodeParent
     private $name;
     private $subParents=array();
     private $children=array();

     function setName($name){
         $this->name=$name;
     }
     function addChild($child){
         $this->children[] = $child;
     }
     function addParent($parent){
         $this->subParents[] = $parent;
     }
     function getName(){
         return $this->name;
     }
     function getSubParents(){
         return $this->subParents;
     }
     function getChildren(){
         return $this->children;
     }
}

元素应添加到数组中,您当前正在使用
NodeParent
实例覆盖数组。可以通过以下方法推送到阵列:

class NodeParent
     private $name;
     private $subParents=array();
     private $children=array();

     function setName($name){
         $this->name=$name;
     }
     function addChild($child){
         $this->children[] = $child;
     }
     function addParent($parent){
         $this->subParents[] = $parent;
     }
     function getName(){
         return $this->name;
     }
     function getSubParents(){
         return $this->subParents;
     }
     function getChildren(){
         return $this->children;
     }
}