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