Blog Post RSS ?

Blogs » PHP » Lies! Darn Lies!
 

Lies! Darn Lies!

by Harry Fuecks

Been talking to Marcus Boerger, via email, about the PHP 5 Standard Library article and need to confess ;)

Marcus makes a very good point about the DirectoryReader example I used;

Your DirectoryReader is wrong. It does the non file skipping in valid, but it should be done in next().

But after the necessary changes it still has a major design flaw. It uses recursion. And Iterators are about unfolding.

So what you should do is preventing that recursion and if you look at the result and how much it took you to complete it correctly you see why using FilterIterator would have been better. That one can be used to filter out the non files easily and it makes clear why iterators can be used for functional programming – in this scenario the filter algorithm provided by FilterIterator doesn’t know anything about your data.

Here’s the problem class plus code using it;

class DirectoryReader extends DirectoryIterator { function __construct($path) { parent::__construct($path); } function current() { return parent::getFileName(); } function valid() { if ( parent::valid() ) { if ( !parent::isFile() ) { parent::next(); return $this->valid(); } return TRUE; } return FALSE; } function rewind() { parent::rewind(); } } // Create a directory reader for the current directory $Reader = new DirectoryReader('./'); // Loop through the files in the directory ?!? foreach ( $Reader as $Item ) { echo $Item.'
'; }

Anyone want to take up the guantlet and re-implement?

As I understand what Marcus is saying, the alternative is to use DirectoryIterator itself then pass it to something extending FilterIterator (or perhaps better extending DirectoryFilterDots) – the “spec” is for something to list only the files in a given directory, ignoring the parent and current nodes.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Ping.fm
  • Twitthis

Related posts:

  1. How to Use PHP Namespaces, Part 3: Keywords and Autoloading In the final part of his series explaining PHP namespaces,...
  2. FeedScrub Launches New RSS Filter – 500 Invites If you're an avid RSS user, then you've probably felt...
  3. Build Your Own WordPress Themes the Easy Way A custom WordPress theme is a great service to sell...
  4. Techy Treasures #3: When is a mouseout not a mouseout? I've had this little gadget in my toolbox for a...
  5. Introducing Bucket: A Minimal Dependency Injection Container for PHP Troels got fed up with the lack of a decent...

This post has 8 responses so far

Sponsored Links

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.

Follow SitePoint on...