I have a lot of classes in multiples subfolders that I load using this autoloader :
spl_autoload_register(function ($class) {
$class = str_replace('\\', DIRECTORY_SEPARATOR, strtolower($class));
if(file_exists(FILES_PATH.'classes/'.$class.'.class.php')){
require_once(FILES_PATH.'classes/'.$class.'.class.php');
}
});
So if i do new Folder\subFolder\Myclass, it works.
The classes in folders are all in a namespace.
All these classes must use the database class, and the problem is here : When the class is in a namespace and search the database class, it can't find it.
(DB class is in global namespace)
So I try to put "use BDD" (Bdd is the db class) and it still doesn't work, because Bdd is using PDO and so i must do "use bdd, pdo;" in EVERY classes of the project...
I find this stupid. Is this normal ? Is there a better way to autoload, without using namespaces ?
\BDD. It should solve that problem.useimports. No, there's no way around that. No, this is not "stupid". I don't understand why you need touse PDOin every file that usesBDDthough.PDOanywhere in the file? If not, you don't need touseit. If you're usingBDDand it's not in the same namespace as the file, then of course you need to either use fully qualified names (see Rocket's answer) oruseit.