AdRock Posted September 2, 2013 Share Posted September 2, 2013 I have recently started using OOP for my old procedural code and I'm getting the hang of it. The problem I have is with autoloading my class files. I have this code on each of my pages I'm redoing. require("php/classes/dbConstants.php"); function __autoload($className) { $extensions = array(".php", ".class.php", ".inc"); $paths = explode(PATH_SEPARATOR, get_include_path()); $className = str_replace("_" , DIRECTORY_SEPARATOR, $className); foreach ($paths as $path) { $filename = $path . DIRECTORY_SEPARATOR . $className; foreach ($extensions as $ext) { if (is_readable($filename . $ext)) { require_once $filename . $ext; break; } } } } try { $forum = new Database(DBHOST, DBUSER, DBPASS, DBNAME); } catch (Exception $e) { echo $e->getMessage(), "\n"; } but i'm getting this error message Fatal error: Class 'Database' not found in C:\www\newOB\news.php on line 21 The file with the autoloader is in the root and the database class is in a folder called php and in a subfolder called classes. I need to adjust the path of where it's looking for my classes but no idea how Can anyone please help? Quote Link to comment https://forums.phpfreaks.com/topic/281780-class-autoloading/ Share on other sites More sharing options...
trq Posted September 2, 2013 Share Posted September 2, 2013 No need to reinvent the wheel here IMO. Composer ships with a perfectly capable autoloader, use it. Quote Link to comment https://forums.phpfreaks.com/topic/281780-class-autoloading/#findComment-1447807 Share on other sites More sharing options...
objnoob Posted September 2, 2013 Share Posted September 2, 2013 I would choose a single naming convention for my class files. Then I can....... function __autoload($className) { include $path_to_class_directory . '/' . $className . '.class.php'; } Anyways, You can (if you insist) ....... function __autoload($className) { $extensions = array(".php", ".class.php", ".inc"); $paths = explode(PATH_SEPARATOR, get_include_path()); # ######################################### // ADD CLASSES DIR TO THE $paths ARRAY # ######################################### $paths[] = '/documentRoot/php/classes'; $className = str_replace("_" , DIRECTORY_SEPARATOR, $className); foreach ($paths as $path) { $filename = $path . DIRECTORY_SEPARATOR . $className; foreach ($extensions as $ext) { if (is_readable($filename . $ext)) { require_once $filename . $ext; break; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/281780-class-autoloading/#findComment-1447809 Share on other sites More sharing options...
Hall of Famer Posted September 2, 2013 Share Posted September 2, 2013 Also a friendly tip, try to avoid using __autoload($class) unless you are developing a really small application, instead look for spl_autoload_register($loader). Quote Link to comment https://forums.phpfreaks.com/topic/281780-class-autoloading/#findComment-1447850 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.