Loading all files within a directory

Loading files from a directory – the historical way

The glob() function has three possible return values:

  • Array( filenames ) => success
  • Array() empty => nothing found
  • FALSE => Error

So the correct check would be

$files = glob( sprintf( 
    '%s/postypes/*.php',
    get_stylesheet_directory()
) );
if ( empty( $files )
{
    _e( 'No PHP files found', 'your-textdomain' );
}
elseif ( ! $files )
{
    _e( 'Shrug! There went something terribly wrong!', 'your-textdomain' );
}
else
{
    foreach ( $files as $file )
        include_once $file;
}

…buuuut…

The current days way to fetch & load files – PHP 5.3+

Since we are already reaching the end of life of PHP 5.3 support, we can assume that someone at least got PHP 5.3.x installed and therefore has the SPL (Standard PHP Library) extension installed – even Ubuntu 12.04 LTS (Long term support) has 5.3 packaged / Ubuntu current is v14. Even when you are running PHP 5.2.x, the SPL has to be forcefully turned off.

And the SPL ships with the DirectoryIterator. So a good setup to load files would look like this:

$it = new DirectoryIterator( sprintf( 
    '%s/postypes/*.php',
    get_stylesheet_directory()
) );

$it->rewind();
foreach ( $it as $fileInfo )
{
    if ( $fileInfo->isDot() )
        continue;

    include_once $fileInfo->getFileName();
}

Extended …

(Edit) For some reason, I mixed the DirectoryIterator with the FilesystemIterator. Of course you should use the later for PHP 5.3 as the later one is a follow up development of the first one.

The nice thing is, that the FilesystemIterator as well as the RecursiveDirectoryIterator has FLAGS, so you can – instead of using the isDot() method – simply set a flag:

$it = new FilesystemIterator( __DIR__.'/assets', FilesystemIterator::SKIP_DOTS );
$it = new RecursiveDirectoryIterator( __DIR__.'/assets', FilesystemIterator::SKIP_DOTS );

Here’s a different example with a little more detailed checks:

# Assume we got this in the functions.php file of a theme
# and we want to load PHP vendor files.
$it = new FilesystemIterator(
    __DIR__.'/vendor/*.php',
    FilesystemIterator::SKIP_DOTS
    | FilesystemIterator::CURRENT_AS_FILEINFO
);

$it->rewind();
while ( $it->valid() )
{
    if (
        $it->isReadable()
        AND $it->isFile()
        AND $it->current() instanceof \SplFileInfo
    )
        include_once $it->current()->getFilename()

    $it->next();
}

You could as well search through different directories using the RecursiveDirectoryIterator and adding your pattern there. Make sure to use a max depth to not overdo it with a search.

What you could do as well, is using a NoRewindIterator, which takes an Iterator or something else that implements Traversable and loops over it. This way you can make sure that you won’t iterator over a single file more than once.

$it = new RecursiveDirectoryIterator(
    __DIR__.'/vendor/*.php',
    FilesystemIterator::SKIP_DOTS
    | FilesystemIterator::CURRENT_AS_FILEINFO
);
$it->rewind();
$files = new NoRewindIterator( $it );
$files->rewind();
foreach ( $files as $file )
    # ...

but that may just be too much.