On Mon, Feb 23, 2009 at 9:31 AM, Ryan Panning <rpann...@gmail.com> 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

Reply via email to