Php 在有根有向无环树中,将父门的后代作为子对象(节点和门)的一维数组获取
我的有根有向无环树将由逻辑门(and、or、xor…)和节点组成 一个节点和一个门,每个都是一个对象 只有门可以是父级 每个门对象的子属性都是公共的。子对象可以是对象数组,也可以是空数组 下面是我的案例的有效树。(g:门,n:节点) 我的目标 以对象数组的形式获取门的子体。(例如:Php 在有根有向无环树中,将父门的后代作为子对象(节点和门)的一维数组获取,php,oop,object,tree,descendant,Php,Oop,Object,Tree,Descendant,我的有根有向无环树将由逻辑门(and、or、xor…)和节点组成 一个节点和一个门,每个都是一个对象 只有门可以是父级 每个门对象的子属性都是公共的。子对象可以是对象数组,也可以是空数组 下面是我的案例的有效树。(g:门,n:节点) 我的目标 以对象数组的形式获取门的子体。(例如:print\r($gate1->getsubstands())) 我的问题 这是我的第一次oop体验,我尝试制作一个与我的工作相关的小应用程序。在我下面的代码中,我了解到有问题的部分是:$this->subjectio
print\r($gate1->getsubstands())
)
我的问题
这是我的第一次oop体验,我尝试制作一个与我的工作相关的小应用程序。在我下面的代码中,我了解到有问题的部分是:$this->subjections[]=$obj代码>
如果我将此行更改为$subjects
,则由于变量问题的范围,输出仍然不正确
如何使$gate1->getsubstands()
正常工作
预期输出正确
下面树的7个子对象的一维数组。(g:门,n:节点)
我得到的输出不正确
Array
(
[0] => Node Object
(
[id] => 1
)
[1] => Node Object
(
[id] => 2
)
[2] => Node Object
(
[id] => 3
)
[3] => Gate Object
(
[id] => 2
[type] => or
[desc] => My First OR Gate
[children] => Array
(
[0] => Node Object
(
[id] => 4
)
[1] => Node Object
(
[id] => 5
)
)
[descendants] => Array
(
[0] => Node Object
(
[id] => 4
)
[1] => Node Object
(
[id] => 5
)
)
)
[4] => Gate Object
(
[id] => 3
[type] => xor
[desc] => My First XOR Gate
[children] => Array
(
)
[descendants] =>
)
)
代码:类节点、类门、try.php
class Node
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
require_once('Node.php');
require_once('Gate.php');
$node1 = new Node(1);
$node2 = new Node(2);
$node3 = new Node(3);
$node4 = new Node(4);
$node5 = new Node(5);
$gate1 = new Gate(1,'and','My First AND Gate');
$gate2 = new Gate(2,'or','My First OR Gate');
$gate3 = new Gate(3,'xor','My First XOR Gate');
$gate1->addChild($node1);
$gate1->addChild($node2);
$gate1->addChild($node3);
$gate1->addChild($gate2);
$gate1->addChild($gate3);
$gate2->addChild($node4);
$gate2->addChild($node5);
function pa($var)
{
echo '<pre>';print_r($var);echo '</pre>';
}
/**
* get top gate's descandants
* (not only 1st level,
* but children @all levels)
*/
pa($gate1->getDescendants());
等级门
class Gate
{
public $id;
public $type;
public $desc;
public $children = array();
public $descendants;
public function __construct($id, $type, $desc)
{
$this->id = $id;
$this->type = $type;
$this->desc = $desc;
}
public function addChild($child)
{
if($child instanceof Node OR $child instanceof Gate)
{
$this->children[] = $child;
}
else
{
throw new Exception('Child of Gate must be a Node or Gate object!');
}
}
public function getDescendants()
{
if(!empty($this->children))
{
$count_children = count($this->children);
for ($i = 0; $i < $count_children; $i++)
{
$obj = $this->children[$i];
$this->descendants[] = $obj;
// i tried also below
// $descendants[] = $obj;
if($obj instanceof Gate)
{
$obj->getDescendants();
}
}
return $this->descendants;
// i tried also below
//return $descendants;
}
else
{
return $this->children;
}
}
}
类门
{
公费$id;
公共$类型;
公帑$desc;
public$children=array();
公营机构;;
公共函数构造($id,$type,$desc)
{
$this->id=$id;
$this->type=$type;
$this->desc=$desc;
}
公共函数addChild($child)
{
if($child instanceof Node或$child instanceof Gate)
{
$this->children[]=$child;
}
其他的
{
抛出新异常('Gate的子级必须是节点或Gate对象!');
}
}
公共函数getDescents()
{
如果(!empty($this->children))
{
$count\u children=count($this->children);
对于($i=0;$i<$count_儿童;$i++)
{
$obj=$this->children[$i];
$this->substands[]=$obj;
//我在下面也试过了
//$substands[]=$obj;
if($obj门实例)
{
$obj->getsubstands();
}
}
返回$this->子代;
//我在下面也试过了
//返回$子代;
}
其他的
{
返回$this->children;
}
}
}
try.php
class Node
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
require_once('Node.php');
require_once('Gate.php');
$node1 = new Node(1);
$node2 = new Node(2);
$node3 = new Node(3);
$node4 = new Node(4);
$node5 = new Node(5);
$gate1 = new Gate(1,'and','My First AND Gate');
$gate2 = new Gate(2,'or','My First OR Gate');
$gate3 = new Gate(3,'xor','My First XOR Gate');
$gate1->addChild($node1);
$gate1->addChild($node2);
$gate1->addChild($node3);
$gate1->addChild($gate2);
$gate1->addChild($gate3);
$gate2->addChild($node4);
$gate2->addChild($node5);
function pa($var)
{
echo '<pre>';print_r($var);echo '</pre>';
}
/**
* get top gate's descandants
* (not only 1st level,
* but children @all levels)
*/
pa($gate1->getDescendants());
require_once('Node.php');
一次需要_('Gate.php');
$node1=新节点(1);
$node2=新节点(2);
$node3=新节点(3);
$node4=新节点(4);
$node5=新节点(5);
$gate1=新门(1,'and','My First and Gate');
$gate2=新门(2,'or','My First or Gate');
$gate3=新门(3,'xor','My First xor Gate');
$gate1->addChild($node1);
$gate1->addChild($node2);
$gate1->addChild($node3);
$gate1->addChild($gate2);
$gate1->addChild($gate3);
$gate2->addChild($node4);
$gate2->addChild($node5);
功能pa($var)
{
回显“”;打印($var);回显“”;
}
/**
*获取top gate的说明
*(不仅是第一级,
*但儿童(所有级别)
*/
pa($gate1->getsubstands());
一次小调整:
调用$obj->getsubstands()
时,您没有使用返回的值
我假设您希望它合并到后代变量中,因为您正在请求一个7元素数组响应。非常感谢您以光速给出的答案,我已经处理了2天。有时,您只需要另一双眼睛就能看到您反复阅读的内容。很乐意帮忙。
if($obj instanceof Gate)
{
$this->descendants = array_merge($this->descendants, $obj->getDescendants());
}