【发布时间】:2016-05-31 17:05:12
【问题描述】:
我有一个 foreach 实例化了几种类,它必须是嵌套的。它们都扩展了同一个抽象类。
我不需要处理任何具体案例,而是所有案例。
我应该如何处理这种嵌套?我正在考虑在执行 foreach 的类中定义一个函数,或者在 Parent 类中定义一个函数,其中定义了所有这些情况。
示例 1
<?php
class Parent
{
private $childs = [];
function nest($child)
{
$this->childs[] = $child;
}
}
// There is different types of A that are nested according to its type.
$A1 = class A extends Parent { $type = 1 }
$A2 = class A extends Parent { $type = 2 }
$B = class B extends Parent {}
$A3 = class A extends Parent { $type = 2 }
$C = class C extends Parent {}
// foreach ([$A1, $A2, $B, $A3, $C])
// $A1->nest($A2);
但是$A1不能嵌套,$A2只能被$A1嵌套,$B可以被$A1和$A2嵌套但是应该被$A2嵌套,因为它是前一个对象可以嵌套的,$A3应该嵌套到$A1等等
// expected output of the example
object(A)#1 (2) {
["type"]=>
int(1)
["childs":"Parent":private]=>
array(2) {
[0]=>
object(A)#2 (2) {
["type"]=>
int(2)
["childs":"Parent":private]=>
array(1) {
[0]=>
object(B)#3 (2) {
["childs":"Parent":private]=>
array(0) {
}
}
}
}
[1]=>
object(A)#4 (2) {
["type"]=>
int(2)
["childs":"Parent":private]=>
array(1) {
[0]=>
object(C)#5 (1) {
["childs":"Parent":private]=>
array(0) {
}
}
}
}
}
}
示例 2
<?php
$numbers = [1, 2, 3, 2, 4];
class Number
{
private $biggers = [];
public function addBigger($number)
{
$this->biggers[] = $number;
}
}
class One extends Number{ private $value = 1; }
class Two extends Number{ private $value = 2; }
class Three extends Number{ private $value = 3; }
class Four extends Number{ private $value = 4; }
foreach ($numbers as $number) {
// Algorithm
}
预期输出:
object(One)#1 (2) {
["value":"One":private]=>
int(1)
["biggers":"Number":private]=>
array(2) {
[0]=>
object(Two)#2 (2) {
["value":"Two":private]=>
int(2)
["biggers":"Number":private]=>
array(1) {
[0]=>
object(Three)#3 (2) {
["value":"Three":private]=>
int(3)
["biggers":"Number":private]=>
array(0) {
}
}
}
}
[1]=>
object(Two)#4 (2) {
["value":"Two":private]=>
int(2)
["biggers":"Number":private]=>
array(1) {
[0]=>
object(Four)#5 (2) {
["value":"Four":private]=>
int(4)
["biggers":"Number":private]=>
array(0) {
}
}
}
}
}
}
【问题讨论】:
-
嵌套到底是什么意思?
-
也不明白.. 会嵌套谁和什么?嵌套在视觉上看起来如何?
$type代表什么?想要完成什么? -
为了清楚起见,我添加了父定义
-
澄清一下,你期望什么输出?
-
@FelippeDuarte 我添加了预期的输出
标签: php algorithm oop design-patterns