Re: DirectoryIterator
| From: | Nathan Nobbe | Date: | Tue, 26 Jan 2010 20:33:23 +0000 |
| Subject: | Re: DirectoryIterator | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-301490@lists.php.net to get a copy of this message | ||
On Tue, Jan 26, 2010 at 1:25 PM, Christoph Boget <cboget@hotmail.com> wrote:
> >
> > def not on DirectorIterator afaik, and furthermore, i dont think thats
> > supported at the shell / filesystem level even.
>
>
> Well if the Iterator has the whole of the collection in order to be able to
> iterate over it, I would think that it should be able to return the size of
> that collection... :(
>
right but the collection is built during iteration. one thing you could do
if you wanted it to *appear* as though the count was there at construction
is to subclass and iterate once in the constructor.
something like this:
<?php
class SmartDirectoryIterator extends DirectoryIterator implements Countable
{
private $_iCount = 0;
public function __construct($sPath)
{
parent::__construct($sPath);
foreach($this as $oFile)
if($oFile->isFile())
$this->_iCount++;
}
public function count()
{
return $this->_iCount;
}
?>
-nathan