本文介绍了父级、子级、递归列表和方法结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是显示从Person X 开始并显示所有后代的家谱.无需显示兄弟姐妹、父母或其他祖先.

My goal is to display a family tree starting from Person X and showing all descendants. No need to show siblings, parents, or other ancestors.

为此,我有一个 person 类.

To this end I have a person class.

我还有一个包含 person_IDparent_ID 列的数据库表.

I also have a database table with person_ID and parent_ID columns.

当创建 person 类时,您将所需的人员 ID 传递给它,然后它会从该表中预加载其父和子 ID.

When the person class is created you pass it the desired person ID, then it will preload its parents and children IDs from that table.

为了创建后代树,我在 person 类中编写了以下方法:

In order to create the descendents tree I wrote the following method inside the person class:

public function loadChildren() {
    foreach ($this->_ChildIDs as $curChildID) {
        $child = new Person();
        $child->loadSelfFromID($curChildID);
        $child->loadChildren();
        $this->_Children[] = $child;
    }
}

这成功地递归加载了整个后代树.

This successfully recursively loads the entire tree of descendents.

到目前为止一切顺利.

为了将此数据显示为嵌套的 HTML 列表,我编写了以下独立函数:

In order to display this data as a nested HTML list I wrote the following standalone function:

function recursivePrint($array) {
    echo '<ul>';
    foreach ($array as $child) {
        echo '<li>'.$child->getName();
        recursivePrint($child->getChildren());
        echo '</li>';
    }
    echo '</ul>';
}

最终脚本如下所示:

$c = new Person();
$c->loadSelfFromID('1');
$c->loadChildren(); // recursively loads all descendants
$descendants = $c->getChildren();
recursivePrint($descendants);

//Output is as expected.

我的问题是:我应该把那个独立的功能放在哪里?

它是否只是被扔到一个随机实用程序中,包括那些实际上不会去其他任何地方的函数?它应该进入 person 类吗?它应该进入一个只生成树的 FamilyTree 类吗?

Does it just get tossed in a random utility include for functions that don't really go anywhere else? Should it go into the person class? Should it go into a FamilyTree class that only makes trees?

推荐答案

您可以利用复合设计模式.

这是另一个资源:http://devzone.zend.com/364/php-patterns_the-composite-pattern/

编辑

class Person
{
    public $name;
    protected $_descendants = null;

    public function __construct($name)
    {
        $this->name = $name;
        $this->_descendants = array();
    }

    public function addDescendant(Person $descendant)
    {
        $i = array_search($descendant, $this->_descendants, true);

        if($i === false){
            $this->_descendants[] = $descendant;
        }
    }

    public function toString()
    {
        $str = $this->name;
        if(count($this->_descendants) > 0){

            $str .= PHP_EOL . '<ul>' . PHP_EOL;

            foreach($this->_descendants as $descendant){
                $str .= '<li>' .  $descendant->toString() . '</li>' . PHP_EOL;
            }
            $str .= '</ul>' . PHP_EOL;

        }
        return $str;
    }
}


$dad = new Person('Dad');
$dad->addDescendant(new Person('Big sister'));
$dad->addDescendant(new Person('Big brother'));
$dad->addDescendant(new Person('Little brat'));

$grandMa = new Person('GrandMa');

$grandMa->addDescendant($dad);

echo $grandMa->toString();

这篇关于父级、子级、递归列表和方法结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 22:12