PHP object parent/child recursion

烂漫一生 提交于 2019-12-04 05:46:51

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

Gordon

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!