08 September, 2010

Load all class at once in php

Loading all the classes at once in php can be done by a simple code. If we have requirement to load lots of php classes
at once then this can be really boring. So in order to load all class at once in php we could write some code. Following
php code does the job.

function loadModules($dir) {
//create an array to hold the class names
$classes = array();
//usign DirectoryIterator class of php to get the handle of directory that contains the classes
$dh = new DirectoryIterator($dir);
//loop through each file
foreach($dh as $file) {
//check if the file is not a directory and ends with a .php extension
if($file->isDir() == 0 && preg_match("/[.]php$/", $file)) {
//include the class
include_once($dir."/".$file);
$class = preg_replace("/[.]php$/","", $file);
$classes []= $class;
}
}
return $classes;
}
?>

1 comment:

Digital Knowledge said...

ah ... your class make it simple when reading file and php class :)

thanks