PHP 对象父/子递归
我有一个父子面向对象关系。父对象有许多子对象,每个子对象都通过引用知道它的父对象。
父级也可以是子级(基本上是一棵树)。
当我在根对象上执行 var_dump() 时,它多次显示 ["parent"]=>RECURSION 并且生成的描述将真的很长。
我想知道我是否做错了什么。如果是,我对“最佳实践”感兴趣。
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你没有做错任何事;您有一个对其子级有引用的父级,并且每个子级都有对其父级的引用。当您
var_dump()
根对象时,它会迭代子对象以打印它们,并且由于每个子对象都有对父对象的引用,因此它会返回。因为这通常会导致无限循环(父 -> 子 -> 父 -> 子 -> ...),PHP 会保留一个它已经访问过的对象的列表,当它遇到一个时,它不会'不要尝试再次转储它,而是打印“RECURSION”。唯一需要注意的是 PHP 使用引用计数来进行垃圾收集,并且像这样的循环结构本身不会解析。因此,您的脚本将泄漏内存,这可能是也可能不是问题。要解决这个问题,您需要手动清理:在父对象超出范围之前,您需要将所有父指针设置为 null。
另请参阅:http://bugs.php.net/bug.php?id=33595
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
var_dump
函数递归地遍历对象图并打印对象的所有可访问数据。现在尝试将下图转化为简单的英语。如果 PHP 不够聪明,无法检测到这种递归,它就会无限运行。因此,它会识别出之前已转储该对象并转储
RECURSION
。你没有做错任何事。点击此处查看其他说明
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.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
避免递归引用的唯一方法是构建“反向树”,这仅在您想要在不知道兄弟姐妹的情况下从子级搜索到父级时有用。就像:
因此,从
$c
开始,您可以跟踪到根节点,即$a
,而无需递归引用。如果您需要从根节点到子节点,那么除了您已经所做的之外没有其他解决方案,这是正确的。
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:
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.