How to include() all PHP files from a directory?

occhiso picture occhiso · Mar 1, 2009 · Viewed 236.4k times · Source

In PHP can I include a directory of scripts?

i.e. Instead of:

include('classes/Class1.php');
include('classes/Class2.php');

is there something like:

include('classes/*');

Couldn't seem to find a good way of including a collection of about 10 sub-classes for a particular class.

Answer

Karsten picture Karsten · Mar 1, 2009
foreach (glob("classes/*.php") as $filename)
{
    include $filename;
}