hoping you can help me out with this question!
Index.php
include_once 'files.class.php';
$file_object = new FileObject('resources');
$file_object->ReturnCurrentDirectoryList();
files.class.php
class FileObject{
public $directory_list;
function __construct($current_directory_in){
$this->directory_list = $this->BuildCurrentDirectoryList($current_directory_in);
}
function BuildCurrentDirectoryList($current_directory_in){
$i = 0;
$iterator = new DirectoryIterator($current_directory_in);
foreach ($iterator as $fileinfo){
if ($fileinfo->isDir()){
$this->directory_list[$i]['pathname'] = $fileinfo->getPathname();
}elseif($fileinfo->isFile()){
$this->directory_list[$i]['filename'] = $fileinfo->getFilename();
}
$i++;
}
}
function ReturnCurrentDirectoryList(){
var_dump($this->directory_list);
}
}
At the end of all this, what is returned is
null
but what should be returned is
array 0 => array 'pathname' => string 'resources\.', 1 => array 'pathname' => string 'resources\..', 2 => array 'pathname' => string 'resources\Images'
I’m somewhat new to classes/methods..
This is wrong:
You assign to
$this->directory_listbutBuildCurrentDirectoryListdoes not return anything. The function have side-effects only, no return value.Remove the assignment so the constructor looks like this and you should be good to go: