我使用此递归代码读取另一个目录中的所有目录,并将它们存储在父目录中。
protected function readDirs($parent)
{
$currentDir = $parent->source();
$items = scandir($currentDir);
foreach ($items as $itemName)
{
if (Dir::isIgnorable($itemName) )
continue;
$itemPath = $currentDir.SLASH.$itemName;
if (! is_dir($itemPath) )
continue;
$item = new ChangeItem(TYPE_DIR);
$item->parent($parent)->source($itemPath);
$parent->children[ $itemName ] = $item;
$this->readDirs($item);
}
}
完成此操作后,如果我对存储其他所有内容的全局对象执行print_r()
,则对于其中的某些项目:
[parent:protected] => ChangeItem Object
*RECURSION*
这是什么意思?我是否能够访问父对象?
最佳答案
这意味着该属性是对 print_r
已访问过的对象的引用。 print_r
检测到这一点并且不会继续沿该路径前进;否则,结果输出将是无限长的。
在程序的上下文中,因为 scandir
还会返回对当前目录和父目录的引用(分别名为 .
和 ..
),遵循它们将导致递归。遵循符号链接(symbolic link)也可能导致递归。
关于php - print_r 输出中的 "*RECURSION* "是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5786067/