将PHP递归函数转换为PHP公共函数

将PHP递归函数转换为PHP公共函数,php,Php,我有以下功能: function copyFolderFiles($src, $dest){ if(!is_dir($src)){ return false; } if(!is_dir($dest)) { if(!mkdir($dest)) { return false; } } $i = new DirectoryIterator($src); foreach(

我有以下功能:

function copyFolderFiles($src, $dest){
    if(!is_dir($src)){
        return false;
    } 
    if(!is_dir($dest)) { 
        if(!mkdir($dest)) {
            return false;
        }    
    }

    $i = new DirectoryIterator($src);
    foreach($i as $f) {
        if(is_dir($f) != $dest){
            if($f->isFile()) {
                copy($f->getRealPath(), ($dest.'/'. $f->getFilename()));
            } else if(!$f->isDot() && $f->isDir()) {
                copyFolderFiles($f->getRealPath(), ($dest.'/'. $f));
            }
        }
    }
}
我尝试将其转换为公共类递归函数,如下所示,但没有成功:

class systemData {
    public function copyFolderFiles($src, $dest, $n = 0){
        if(!is_dir($src)){
            return false;
        } 
        if(!is_dir($dest)) { 
            if(!mkdir($dest)) {
                return false;
            }    
        }

        $i = new DirectoryIterator($src);
        foreach($i as $f) {
            if(is_dir($f) != $dest){
                if($f->isFile()) {
                    copy($f->getRealPath(), ($dest.'/'. $f->getFilename()));
                } else if(!$f->isDot() && $f->isDir()) {
                    copyFolderFiles($f->getRealPath(), ($dest.'/'. $f), $n+1);
                }
            }
        }
    }

}
我得到了
未定义的函数copyFolderFiles()
引用了
copyFolderFiles($f->getRealPath(),($dest.'/'.$f),$n+1)

我不确定我做错了什么来转换它。上述函数用于将所有文件和文件夹复制到特定目录中。它作为一个独立函数工作,但我希望它作为一个公共类中的函数

我按如下方式调用该函数:

$systemData = new systemData;
$systemData->copyFolderFiles($documentRoot,$tempFileBackupDir);
改变


当前代码试图调用不存在的全局函数
copyFolderFiles

您需要通过
$this->
引用类方法:

class systemData
{

    public function copyFolderFiles($src, $dest, $n = 0)
    {
        if(!is_dir($src)){
            return false;
        }
        if(!is_dir($dest)) {
            if(!mkdir($dest)) {
                return false;
            }
        }

        $i = new DirectoryIterator($src);
        foreach($i as $f) {
            if(is_dir($f) != $dest){
                if($f->isFile()) {
                    copy($f->getRealPath(), ($dest.'/'. $f->getFilename()));
                } else if(!$f->isDot() && $f->isDir()) {
                    //HERE
                    $this->copyFolderFiles($f->getRealPath(), ($dest.'/'. $f), $n+1);
                }
            }
        }
    }

}

您是否创建了实例/对象。演示如何调用函数它现在是一个对象,因此需要先实例化它,然后使用OOP调用它syntax@scaisEdge我更新了我的问题-
$systemData=newsystemdata$systemData->copyFolderFiles($documentRoot,$tempFileBackupDir)
$this->copyFolderFiles($f->getRealPath(), ($dest.'/'. $f), $n+1);
class systemData
{

    public function copyFolderFiles($src, $dest, $n = 0)
    {
        if(!is_dir($src)){
            return false;
        }
        if(!is_dir($dest)) {
            if(!mkdir($dest)) {
                return false;
            }
        }

        $i = new DirectoryIterator($src);
        foreach($i as $f) {
            if(is_dir($f) != $dest){
                if($f->isFile()) {
                    copy($f->getRealPath(), ($dest.'/'. $f->getFilename()));
                } else if(!$f->isDot() && $f->isDir()) {
                    //HERE
                    $this->copyFolderFiles($f->getRealPath(), ($dest.'/'. $f), $n+1);
                }
            }
        }
    }

}