如何在PHP中处理嵌套(递归迭代器)动态对象?
我有一个foreach,它引用了几种类型的类,这必须是嵌套的。它们都是同一个抽象类的扩展 我不需要处理任何具体的案件,但所有的案件 我应该如何处理这种嵌套?我正在考虑在类中定义一个函数来执行foreach,或者在如何在PHP中处理嵌套(递归迭代器)动态对象?,php,algorithm,oop,design-patterns,Php,Algorithm,Oop,Design Patterns,我有一个foreach,它引用了几种类型的类,这必须是嵌套的。它们都是同一个抽象类的扩展 我不需要处理任何具体的案件,但所有的案件 我应该如何处理这种嵌套?我正在考虑在类中定义一个函数来执行foreach,或者在父类中定义一个函数,该类中定义了所有这些情况 例1 例2 你的意思是这样的: $objectArray[] = $A1; $objectArray[] = $A2; $objectArray[] = $B; $objectArray[] = $A3; $objectArray[] = $
父类中定义一个函数,该类中定义了所有这些情况
例1
例2
你的意思是这样的:
$objectArray[] = $A1;
$objectArray[] = $A2;
$objectArray[] = $B;
$objectArray[] = $A3;
$objectArray[] = $C;
foreach($objectArray as $obj){
if(isset($obj->type)){
//type is defined
}else{
}
}
你的意思是这样的:
$objectArray[] = $A1;
$objectArray[] = $A2;
$objectArray[] = $B;
$objectArray[] = $A3;
$objectArray[] = $C;
foreach($objectArray as $obj){
if(isset($obj->type)){
//type is defined
}else{
}
}
你需要的是一份工作
您必须实现自己的递归迭代器
:
class RecursiveContainerIterator extends IteratorIterator implements RecursiveIterator {
public function getChildren() {
if (is_array($this->current())) {
return new RecursiveArrayIterator($this->current());
} else if ($this->current() instanceof IteratorAggregate) {
return $this->current()->getIterator();
} else {
throw new InvalidArgumentException('');
}
}
public function hasChildren() {
return is_array($this->current()) || $this->current() instanceof Traversable;
}
}
class Container implements IteratorAggregate {
private $children;
public function __construct() {
$this->children = $children = new ArrayObject();
}
public function addChild($child) {
$this->children->append($child);
}
public function getChildren() {
return $this->children;
}
public function getIterator() {
return new RecursiveContainerIterator($this->children);
}
}
$parent = new Container();
$child1 = new Container();
$child2 = new Container();
$grandChild1 = new Container();
$grandChild2 = new Container();
$grandChild3 = new Container();
$grandChild4 = new Container();
$parent->addChild($child1);
$parent->addChild($child2);
$parent->addChild(array(5, 6));
$child1->addChild($grandChild1);
$child1->addChild($grandChild2);
$child2->addChild($grandChild3);
$child2->addChild($grandChild4);
$grandChild1->addChild(1);
$grandChild2->addChild(2);
$grandChild3->addChild(3);
$grandChild3->addChild(4);
foreach (new RecursiveIteratorIterator($parent, RecursiveIteratorIterator::LEAVES_ONLY) as $child) {
var_dump($child);
}
将输出:
如果您需要处理对象本身,而不仅仅是叶子,那么可以先使用递归迭代器::SELF_
标志。您需要的是一个
您必须实现自己的递归迭代器
:
class RecursiveContainerIterator extends IteratorIterator implements RecursiveIterator {
public function getChildren() {
if (is_array($this->current())) {
return new RecursiveArrayIterator($this->current());
} else if ($this->current() instanceof IteratorAggregate) {
return $this->current()->getIterator();
} else {
throw new InvalidArgumentException('');
}
}
public function hasChildren() {
return is_array($this->current()) || $this->current() instanceof Traversable;
}
}
class Container implements IteratorAggregate {
private $children;
public function __construct() {
$this->children = $children = new ArrayObject();
}
public function addChild($child) {
$this->children->append($child);
}
public function getChildren() {
return $this->children;
}
public function getIterator() {
return new RecursiveContainerIterator($this->children);
}
}
$parent = new Container();
$child1 = new Container();
$child2 = new Container();
$grandChild1 = new Container();
$grandChild2 = new Container();
$grandChild3 = new Container();
$grandChild4 = new Container();
$parent->addChild($child1);
$parent->addChild($child2);
$parent->addChild(array(5, 6));
$child1->addChild($grandChild1);
$child1->addChild($grandChild2);
$child2->addChild($grandChild3);
$child2->addChild($grandChild4);
$grandChild1->addChild(1);
$grandChild2->addChild(2);
$grandChild3->addChild(3);
$grandChild3->addChild(4);
foreach (new RecursiveIteratorIterator($parent, RecursiveIteratorIterator::LEAVES_ONLY) as $child) {
var_dump($child);
}
将输出:
如果您需要处理对象本身,而不仅仅是叶子,那么可以先使用递归迭代器::SELF_标志。嵌套到底是什么意思?我也不明白。。谁和什么将被嵌套?嵌套的视觉效果如何?
$type
代表什么?你想要完成什么?我添加了Clarity的父定义来澄清事情,你期望什么输出?@FelippeDuarte我添加了期望的输出你到底说嵌套是什么意思?我也不明白。。谁和什么将被嵌套?嵌套的视觉效果如何?$type
代表什么?你想要完成什么?我添加了Clarity的父定义来澄清事情,你期望什么输出?@FelippeDuarte我在我的示例中添加了期望的输出,每个类都应该扩展容器,对吗?在我的示例中,每个类都应该扩展容器,对吗?
$objectArray[] = $A1;
$objectArray[] = $A2;
$objectArray[] = $B;
$objectArray[] = $A3;
$objectArray[] = $C;
foreach($objectArray as $obj){
if(isset($obj->type)){
//type is defined
}else{
}
}
class RecursiveContainerIterator extends IteratorIterator implements RecursiveIterator {
public function getChildren() {
if (is_array($this->current())) {
return new RecursiveArrayIterator($this->current());
} else if ($this->current() instanceof IteratorAggregate) {
return $this->current()->getIterator();
} else {
throw new InvalidArgumentException('');
}
}
public function hasChildren() {
return is_array($this->current()) || $this->current() instanceof Traversable;
}
}
class Container implements IteratorAggregate {
private $children;
public function __construct() {
$this->children = $children = new ArrayObject();
}
public function addChild($child) {
$this->children->append($child);
}
public function getChildren() {
return $this->children;
}
public function getIterator() {
return new RecursiveContainerIterator($this->children);
}
}
$parent = new Container();
$child1 = new Container();
$child2 = new Container();
$grandChild1 = new Container();
$grandChild2 = new Container();
$grandChild3 = new Container();
$grandChild4 = new Container();
$parent->addChild($child1);
$parent->addChild($child2);
$parent->addChild(array(5, 6));
$child1->addChild($grandChild1);
$child1->addChild($grandChild2);
$child2->addChild($grandChild3);
$child2->addChild($grandChild4);
$grandChild1->addChild(1);
$grandChild2->addChild(2);
$grandChild3->addChild(3);
$grandChild3->addChild(4);
foreach (new RecursiveIteratorIterator($parent, RecursiveIteratorIterator::LEAVES_ONLY) as $child) {
var_dump($child);
}
int(1)
int(2)
int(3)
int(4)
int(5)
int(6)