PHP OOP:流畅的界面和树状图


PHP OOP : Fluent interface and tree graphs

我正在尝试为树对象创建一个流畅的接口

以下是我目前所做工作的一个简化示例:

<?php
class node {
    private $childs = array();
    private $parent;
    public function __construct($parent = null) {
        $this->parent = $parent;
    }
    public function addChild($child) {
        $this->childs[] = $child;
        return $this;
    }
    public function createChild() {
        return $this->addChild(new node($this));
    }
    public function setFoo() {
        /* do something */
        return $this;
    }
}
$root = new node();
$root   ->addChild((new node($root))
            ->setFoo()
        )->addChild((new node($root))
            ->setFoo()
        );
?>

我想减少我创建树的部分。我想做的是这样的事情:

$root->createChild()->setFoo();
$root->createChild()->setFoo();

在一行中。并且不必显式地创建新的节点实例(就像我在第一段使用new运算符的代码中所做的那样)。

我的目标是能够创建任何顺序的树及其任何程度的节点,而不必在代码中放入分号。

与其添加createChild函数,我认为您应该更改构造函数和addChild函数以在数据中一致地建立父/子关系。完成后,addChild函数和构造函数就可以在不使用createChild函数的情况下执行所描述的操作。现在,您的构造函数允许在不同的树和树中的分支之间进行交联,所以这可能无论如何都需要更改。

class node {
    private $childs = array();
    private $parent;
    public function __construct(node $parent = null) {
        if(!is_null($parent)) {
            $parent->addChild($this);
        }
    }
    public function addChild(node $child) {
        $this->childs[] = $child;
        $child->parent = $this;
        return $this;
    }
    public function setFoo() {
        /* do something */
        return $this;
    }
}

有了这个,你可以将新对象链接到树中:

$tree = (new node())->addChild(new node())
                    ->addChild((new node())->setFoo())
                    ->addChild((new node())->addChild(new node())
                                           ->addChild(new node())
                                           ->setFoo()
                    );

尝试使用createChild函数是一种棘手的情况,有时需要父函数,有时需要子函数。你可以使用一个同时包含这两者的返回对象来解决这个问题,但我认为最好避免这种情况。如果您不喜欢"(new node())"语法,那么可以使用静态函数:

public static function create(node $parent = null) {
    return new node($parent);
}

根据你的口味,哪个可能更漂亮:

$tree = node::create()->addChild(node::create())
                      ->addChild(node::create()->setFoo())
                      ->addChild(node::create()->addChild(new node())
                                               ->addChild(new node())
                                               ->setFoo()
                      );

您可以添加这些方法来创建子方法。

public function createManyChild($nbrOfChild) {
    for($i = 0; $i < $nbrOfChild; $i++){
        $this->addChild(new node($this));
    }
    return $this;
}

并使用这样的代码。

$root = new node();
$root->createManyChild(3)->setFoo();