ho provato a implementare una classe semplice per la scansione delle directory con DirectoryIterator!
Fortunatamente non esiste documentazione ufficiale esaustiva
e non riesco a capire perchè non va una certa cosa!
qui prendo uno script funzionante
Codice PHP:
<?php
function getFiles(&$rdi,$depth=0) {
if (!is_object($rdi))
return;
for ($rdi->rewind();$rdi->valid();$rdi->next()) {
if ($rdi->isDot())
continue;
if ($rdi->isDir() || $rdi->isFile()) {
for ($i = 0; $i<=$depth;++$i)
echo '';
echo $rdi->current().'
';
if ($rdi->hasChildren())
getFiles($rdi->getChildren(),1+$depth); //richiamata ricorsiva
}
}
}
getFiles(new RecursiveDirectoryIterator('.'));
?>
ora se il tutto lo piazzo in una classe alcuni metodi non sono riconosciuti e mi da errore
Fatal error: Call to undefined method DirectoryIterator::hasChildren()
e la classe in questione è
Codice PHP:
class clsDirectoryManager{
//directory root
private $dirPath;
function clsDirectoryManager($dirPath){
if(!is_dir($dirPath)){
throw new Exception('Invalid directory path!');
}
$this->dirPath=$dirPath;
}
public function fetchContent(){
$this->getFiles(new DirectoryIterator($this->dirPath));
return;
}
function getFiles(&$rdi,$depth=0) {
if (!is_object($rdi))
return;
for ($rdi->rewind();$rdi->valid();$rdi->next()) {
if ($rdi->isDot())
continue;
if ($rdi->isDir() || $rdi->isFile()) {
for ($i = 0; $i<=$depth;++$i)
echo '';
echo $rdi->current().'
';
if ($rdi->hasChildren()) //qui errore e non capisco perchè
getFiles($rdi->getChildren(),1+$depth);
}
}
}
}
?>
qualcuno riesce a spiegarmi perchè $rdi->hasChildren() genera errore dentro la classe e non fuori???