Is there any chance that there are more than one copy of Common.php
file exists in those folders?
Because your code does not break
after including a class file the autoloader
will continue seeking other files with the same name in the folder tree, and would lead to Fatal error: Cannot redeclare class XXX
error. Adding break
could fix the problem.
// Loop the array.
$isClassFound = false;
foreach($array_directories as $path_directory){
$recursive_directory = new RecursiveDirectoryIterator($path_directory);
foreach (new RecursiveIteratorIterator($recursive_directory) as $filename => $file) {
if(file_exists(WEBSITE_DOCROOT.$file->getPath().'/'.$file_name)){
include WEBSITE_DOCROOT.$file->getPath().'/'.$file_name;
$isClassFound = true;
}
if ($isClassFound) break;
}
if ($isClassFound) break;
}
However, the nature of your autoloader
seems like it should not allow any duplicated class file names. Maybe you can write a name duplication checker to guarantee uniqueness.
EDIT:
I removed the class_exists()
part from my answer because using it doesn't make much sense. Anyway, since you saw that version of my answer, and you asked me where to put class_exists()
via the comment, I'll revive the code sample. you can add the following code at the beginning of the autoloader
.
if (class_exists($class_name,false)) // give false to avoid automatic loading
return;