4
class styleFinder{

function styleFinder(){

}

function getFilesNFolders($folder){

    $this->folder = $folder ;
    if($this->folder==""){
        $this->folder = '.';
    }
    if ($handle = opendir($this->folder)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                echo "$file<br /> ";
                if(is_dir($file)){

                    echo "<b>" . $file . " is a folder</b><br />&nbsp;&nbsp;&nbsp;with contents ";
                    $this::getFilesNFolders($file);
                   # echo "Found folder";
                }
            }
        }
        closedir($handle);
    }
}

} I wan to print out a complete tree of folders and files, the script is going into the first folders and finding the files, then finding any sub folders, but not subfolders of those (and yes there is some). Any ideas please?

3
  • 3
    Why the ::? Use $this->getFilesNFolders($file); for one thing. Commented Aug 24, 2010 at 20:02
  • 1
    Static calls are made using self::method() or parent::method(), but you can't reference object state in a static call (-> is not allowed). Think of static calls as being functional: en.wikipedia.org/wiki/Functional_programming Commented Aug 24, 2010 at 20:14
  • 2
    Except that parent::method() is not actually a static call. It's a call that depends on the access type of the original method (if the original was accessed as an instance, then the parent call will have access to $this). So that's a special case (and a non-intuitive one to say the least)... Commented Aug 24, 2010 at 20:18

5 Answers 5

7
$this::getFilesNFolders($file);

Should Be

$this->getFilesNFolders($file);
Sign up to request clarification or add additional context in comments.

Comments

6

Since PHP 5.1.2 you have this usefull class available: http://www.php.net/manual/en/class.recursivedirectoryiterator.php

Comments

2

Accessing a class function is done like this:

$this->functionName():

Comments

1

Since no one provided that yet, here is the RecursiveDirectoryIterator version of your code:

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('/path/to/directory'),
        RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $fileObject) {
    if($fileObject->isDir()) {
        echo "<strong>$fileObject is a folder:</strong><br>\n";
    } else {
        echo $fileObject, "<br>\n";
    }
}

1 Comment

Thanks Gordon, much more than I'd expected, I had no idea these existed! Thanks everyone else for chipping in too.
0

As others have said, within the method itself, you need to call getFilesNFolders with $this -> getFilesNFolders($file). Also, the way the code is posted you're missing a } at the end, but since there is one starting the text after the code, that is probably a typo. The code below worked for me (I ran via the command line, so added code to indent different directory levels and also to output \n's):

<?php

class StyleFinder{
function StyleFinder(){
}

function getFilesNFolders($folder, $spaces){

    $this->folder = $folder ;
    if($this->folder==""){
        $this->folder = '.';
    }
    if ($handle = opendir($this->folder)) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                if(is_dir($file)){
                    echo $spaces . "<b>" . $file . " is a folder</b><br/>&nbsp;&nbsp;&nbsp;with contents:\n";
                    $this -> getFilesNFolders($file, $spaces . "  ");
                } else {
                    echo $spaces . "$file<br />\n";
                }
            }
        }
        closedir($handle);
    }
}
}

$sf = new StyleFinder();
$sf -> getFilesNFolders(".", "");

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.