Re: RecursiveDirectoryIterator and foreach

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ryan Panning wrote:
I have discovered that when I foreach over a RecursiveDirectoryIterator (see example below) the $item actually turns into a SplFileInfo object. I would expect it to be a RecursiveDirectoryIterator. How do I do a hasChildren() on SplFileInfo?

seems like expected functionality to me, you're looping over the contents of the directory and that can only be a file or a directory - thus SplFileInfo seems correct?

in short you don't call hasChildren() on SplFileInfo, you call it on RecursiveDirectoryIterator.

From the docs:
RecursiveDirectoryIterator::hasChildren — Returns whether current entry is a directory and not '.' or '..'

RecursiveDirectoryIterator::getChildren — Returns an iterator for the current entry if it is a directory

Thus this is how you call getChildren properly:

$dir = new RecursiveDirectoryIterator( dirname(dirname(__FILE__)) );
foreach($dir as $splFileInfo) {
  if( $dir->hasChildren() ) {
    $childDir = $dir->getChildren();
    echo get_class($childDir) . ' ' . $childDir->getPath() .  PHP_EOL;
  }
}

many regards,

Nathan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux