Php 实例变量的OOP问题

Php 实例变量的OOP问题,php,oop,Php,Oop,我的两个类“File”和“Folder”中的一段代码有一些问题。我创建了一个页面,显示服务器空间的内容。因此,我编写了类文件夹,其中包含关于它的信息,如“名称”、“路径”和“子项”。children属性包含此文件夹中的“文件”或“文件夹”数组。所以它是一种递归类。为了得到一个通缉目录的整个结构,我编写了一些递归回溯算法,这些算法为服务器上与我的文件夹结构相同的所有子目录提供了一个对象数组。第二种算法是获取该数组并搜索一个特殊文件夹。如果找到该文件夹,该方法将返回其根路径,如果该文件夹不是该目录的

我的两个类“File”和“Folder”中的一段代码有一些问题。我创建了一个页面,显示服务器空间的内容。因此,我编写了类文件夹,其中包含关于它的信息,如“名称”、“路径”和“子项”。children属性包含此文件夹中的“文件”或“文件夹”数组。所以它是一种递归类。为了得到一个通缉目录的整个结构,我编写了一些递归回溯算法,这些算法为服务器上与我的文件夹结构相同的所有子目录提供了一个对象数组。第二种算法是获取该数组并搜索一个特殊文件夹。如果找到该文件夹,该方法将返回其根路径,如果该文件夹不是该目录的子文件夹,则该算法将返回false。我已经为“Folder”对象测试了所有这些方法,效果很好,但现在我通过更密集地使用脚本检测到了一个错误

/**
 * find an subfolder within the given directory (Recursive)
 */
public function findFolder($name) {

    // is this object the object you wanted
    if ($this->name == $name) {
        return $this->getPath();
    }

    // getting array
    $this->bindChildren();
    $result = $this->getChildren();

    // backtracking part
    foreach($result as $r) {
        // skip all 'Files'
        if(get_class($r) == 'File') {
            continue;   
        } else {
            if($search_res = $r->findFolder($name)) {
                return $search_res;
            }
        }
    }

    // loop runned out
    return false;

}

/**
 * stores all children of this folder
 */
public function bindChildren() {
    $this->resetContent();
    $this->dirSearch();
}

/**
 * resets children array
 */
private function resetContent() {
    $this->children = array();
}

/**
 * storing children of this folder
 */
private function dirSearch() {
    $dh = opendir($this->path);

    while($file = readdir($dh)) {
        if($file !== "" && $file !== "." && $file !== "..") {
            if(!is_dir($this->path.$file)) {
                $this->children[] = new File($this->path.$file);
            } else {
                $this->children[] = new Folder($this->path.$file.'/');
            }
        }   
    }
}
在我的网站中,我首先创建一个新的文件夹对象,然后开始查找“doc”的子文件夹,例如,它被称为“test”。文件夹“test”位于“/var/www/media/username/doc/test4/test/”中

$folder = new Folder('/var/www/media/username/doc/');
$dir = $folder->findFolder('test');
如果我打印出
$dir
它会返回我想要的链接,因为文件夹“test”是“docs”的子文件夹,但返回的链接不正确。它应该是'/var/www/media/username/doc/test4/test',但结果是'/var/www/media/username/doc/test',我试着调试了一下,发现包含所有子项的文件夹列表保留了具有正确链接的对象,但是如果对象
$this
没有正确的路径。我不知道为什么,但是

// backtracking part
foreach($result as $r) {

似乎要更改对象属性。我希望有人能帮助我,并提前表示感谢

不要重新发明轮子。PHP已经有了一个用于此目的的类,名为
recursivedirectoryinterator