[英]Get descendants of a parent gate as a 1-dim array of child objects (nodes & gates) in a rooted directed acyclic tree
my rooted directed acyclic trees will consist of logic gates (and,or,xor ...) and nodes . 我扎根的有向无环树将由逻辑门 (和,或,xor ...)和节点组成 。
a node & a gate , each is an object. 一个节点和一个门 ,每个都是一个对象。
only a gate may be a parent. 只有一个门可以是父母。
each gate object has children property as public . 每个Gate对象都具有public的 子级属性。 children can be array of objects or an empty array . 子级可以是对象数组或空数组 。
below is a valid tree for my cases. 下面是我的案例的有效树。 (g:gate, n:node) (g:gate,n:node)
g1
|---|---|---|---|
n1 n2 n3 g2 g3
|\
n4 n5
my aim 我的目标
get descendants of a gate in form of array of objects. 以对象数组的形式获取门的后代。 (ex: print_r($gate1->getDescendants())
) (例如: print_r($gate1->getDescendants())
)
my question 我的问题
This is my first oop experience, I try to make a small app related to my job. 这是我的第一次体验,我尝试制作一个与我的工作相关的小型应用程序。 In my code below, I understand that problematic part is: $this->descendants[] = $obj;
在下面的代码中,我知道有问题的部分是: $this->descendants[] = $obj;
if I change this line to $descendants
, then the output is still improper because of scope of variable issue. 如果我将此行更改为$descendants
,则由于变量问题的范围,输出仍然不正确。
How can I get $gate1->getDescendants()
working properly? 如何使$gate1->getDescendants()
正常工作?
proper output expected 预期的适当输出
1-dim array of 7 children objects for tree below. 下面的树的7个子对象的1维数组。 (g:gate, n:node) (g:gate,n:node)
g1
|---|---|---|---|
n1 n2 n3 g2 g3
|\
n4 n5
improper output I got 我得到的输出不正确
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] =>
)
)
code: class Node, class Gate, try.php 代码:类节点,类门,try.php
class Node
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Gate 类门
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;
}
}
}
try.php try.php
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());
One minor adjustment: 一项小调整:
if($obj instanceof Gate)
{
$this->descendants = array_merge($this->descendants, $obj->getDescendants());
}
When you call $obj->getDescendants()
you aren't using the returned value. 调用$obj->getDescendants()
您没有使用返回的值。
I am assuming that you want it merged into the descendants variable, as you are requesting a 7-element array response. 我假设您希望将其合并到后代变量中,因为您正在请求7元素数组响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.