Re: RecursiveDirectoryIterator and foreach

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

 



On Mon, Feb 23, 2009 at 9:31 AM, Ryan Panning <rpanning@xxxxxxxxx> 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?
>
> However, if I change it to a non-recursive, DirectoryIterator, $item is
> what I would expect, DirectoryIterator. I've tested this with 5.2.8 and
> 5.3b1. I'm guessing this is an issue with my setup as I'm sure I've gotten
> this to work before...
>
>
> $dir = new RecursiveDirectoryIterator('/path/to/dir');
> foreach ($dir as $item) {
>    print get_class($item) . "\r\n";
> }


if youre trying to do recursive iteration whereby you 'flatten' the tree
structure, drop the RecursiveDirectoryIterator into a
RecursiveIteratorIterator (its for iterating over RecursiveIterators), then
you dont have to bother w/ calling hasChildren() at all.  you probly also
want to look at the constructor args, since by default, it only returns leaf
nodes.

$dir = new RecursiveDirectoryIterator('/path/to/dir');

$itt = new RecursiveIteratorIterator($dir,
RecursiveIteratorIterator::CHILD_FIRST);

> foreach ($itt as $item) {
>    print get_class($item) . "\r\n";
> }


-nathan

[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