You are addressing some different issues here.
Autoloading vs. manual loading...
When you register an autoload function, it is called whenever you instantiate an object. In this function, you are supposed to check what class is being instantiated and provide the corresponding class definition somehow, most often by including a relevant php file. This is convenient, because you make sure that only the classes that need to be instantiated are actually declared, in a JIT manner. Note also that, if your class extends another, then the autoload function will also be called for the parent class, and so on recursively for all ancestor classes.
This is different from manually including the class files. As soon as a myfooclass.php
file is included, the relevant class is declared, regardless of whether it gets instantiated later or not.
...and get_declared_classes()
get_declared_classes()
does what its name implies: it returns an array with all the classes that have been declared at the time you call it. In other words, if you call it from several locations in your script, it's likely you'll get different results, depending on what classes have been declared at the time.
The duplication
I can see duplicate paths in your live server's results, but not the way you describe them. For example, core/model/./
is a duplicate of core/model/
because the former resolves to the latter. But core/model/../
is not a duplicate since it resolves to core/
(it only gets duplicated later on, by core/helper/../
and core/ext/../
).
A good practice would be to use realpath()
which would resolve such issues (and some other ones as well) and provide you with absolute, canonicalized, existing paths. Moreover, you should either check if a path already exists before inserting it in $sub_directories
:
if ($fileObject->isDir())
{
$pathname=$fileObject->getPathname();
$slashed=str_replace('\\', '/', $pathname);
$filtered=preg_replace('~.*?(?=core|local)~i', '', $slashed) .'/';
$canonical=realpath($filtered);
if(!in_array($canonical,$sub_directories)) $sub_directories[] = $canonical;
}
...or do an array_unique()
before using it:
$merged_directories = array_merge($main_directories,$sub_directories);
$merged_directories = array_unique($merged_directories);
print_r($merged_directories);
Of course, these solutions are passive and do not require you to alter your application logic.
Another failsafe would be to use include_once
instead of include
so as to not accidentally include the same file twice.
Oh, and a last note: After you have found the file in question, there's no need to continue searching. Just break
the loop:
if(file_exists($path_directory.$file_name))
{
include_once $path_directory.$file_name;
break; // <-- no point going on
}