PHP object parent/child recursion

2019-04-07 17:37发布

I've got a parent-child OO relationship. Parent obejcts has many child objects and every child object knows about it's parent by reference.

The parent can be a child too (basically its a tree).

When i do a var_dump() on the root object it says ["parent"]=>RECURSION many times and the generated description will be really long.

I'm wondering if i do something wrong. If yes, i'm interested in the "best practice".

Thanks for the help!

3条回答
爷的心禁止访问
2楼-- · 2019-04-07 17:39

The only way you can avoid recursive references is when building a "reversed tree", which is only useful is you want to search from the child to the parent without knowing the siblings. Like:

class Foo {
    protected $parent;

    public function __construct(Foo $parent = null) {
        $this->parent = $parent;
    }

    public function getParent() {
        return $this->parent;
    }
}

$a = new Foo;
$b = new Foo($a);
$c = new Foo($b);

So, from $c you can track up to the root node, being $a, without recursive references.

If you need to go from the root node to the children, then there is no other solution than what you already did, which is correct.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-04-07 17:40

You're not doing anything wrong; you have a parent that has a reference to its children, and each child has a reference back to its parent. When you var_dump() the root object, it iterates over the children to print them, and since each child has a reference to the parent, it walks back up. Because this would normally cause an infinite loop (parent -> child -> parent -> child -> ...), PHP keeps a list of object it has visited already, and when it encounters one, it doesn't try to dump it again but instead prints "RECURSION".

The only thing to look out for with this is that PHP uses reference counting for its garbage collection, and circular constructs like these don't resolve by themselves. As a result, your script will leak memory, which may or may not be a problem. To resolve this, you need to clean up manually: just before the parent object goes out of scope, you need to set all parent pointers to null.

See also: http://bugs.php.net/bug.php?id=33595

查看更多
▲ chillily
4楼-- · 2019-04-07 18:01

The var_dump function walks over your object graph recursively and print all accessible data of your objects. Now try to put the below diagram into plain english.

        has                var_dump:
Parent ----> Child         "The Parent object has a child object"
^              |               "That Child object has a Parent Object"
|______________| has               "That Parent object …"

If PHP wasn't smart enough to detect this recursion, it would run infinitely. So instead, it recognizes it has dumped that object before and dumps RECURSION. You are not doing anything wrong.

Click here for another explanation

查看更多
登录 后发表回答