Cli*_*ote 12 php oop recursion
我正在使用这个递归代码来读取另一个目录中的所有目录,并将它们存储在父目录中.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
完成此操作后,如果我print_r()对存储其他所有内容的全局对象执行了操作,则对于其中的某些项目:
[parent:protected] => ChangeItem Object
*RECURSION*
Run Code Online (Sandbox Code Playgroud)
那是什么意思?我能否访问父对象?
Del*_*ani 17
这意味着该属性是对已经访问过的对象的引用print_r.print_r检测到这一点并且不会继续沿着那条路走; 否则,结果输出将无限长.
在程序的上下文中,由于scandir还返回对当前目录和父目录(分别命名.和..分别)的引用,因此它们将导致递归.以下符号链接也可能导致递归.