Destramic Posted April 26, 2011 Share Posted April 26, 2011 hey guys i have a script ive made which loads classes automatically when called...the script works fine but when i extend the class Autoloader_Exception and it comes back with the error Fatal error: Class 'Autoloader_Exception' not found in C:\www\library\autoloader.class.php on line 3 i might be missing something here but i dont know why the autoloader doesnt load the extended class if someone can please help me how i can extend the class please thanks <?php class Autoloader extends Autoloader_Exception { protected static $_declared_classes = array(); public static function load_class($class_name) { $class_name = ucwords($class_name); $file = self::get_class_path($class_name); try { if (!class_exists($class_name, FALSE)) { if (file_exists($file)) { require_once $file; self::$_declared_classes[] = $class_name; } else { throw new Exception(sprintf("Class '%s' not found.<br />\n", $class_name)); } } } catch (Exception $e) { echo $e->getMessage(); } } protected static function get_class_path($class_name) { global $classes; if (array_key_exists($class_name, $classes)) { return ROOT . DS . $classes[$class_name]; } } public static function declared_classes() { echo "<pre>"; print_r(self::$_declared_classes); echo "</pre>"; } } spl_autoload_register(array('Autoloader', 'load_class')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/ Share on other sites More sharing options...
trq Posted April 26, 2011 Share Posted April 26, 2011 The first question I have is why on earth would your autoloader extend an Exception class? Your autoloader is *NOT* a type of Exception. Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206355 Share on other sites More sharing options...
Destramic Posted April 26, 2011 Author Share Posted April 26, 2011 i know that but it does handle exceptions...i just thought it was a good way of handleing exceptions through another class? Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206364 Share on other sites More sharing options...
trq Posted April 26, 2011 Share Posted April 26, 2011 If the classes are unrelated (which they are) they are unrelated, it really is that simple. Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206367 Share on other sites More sharing options...
Destramic Posted April 26, 2011 Author Share Posted April 26, 2011 ok thanks for your help...but can you give me and example on where extending exceptions would be good within a class....a forms class, mysql class? Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206383 Share on other sites More sharing options...
KevinM1 Posted April 26, 2011 Share Posted April 26, 2011 ok thanks for your help...but can you give me and example on where extending exceptions would be good within a class....a forms class, mysql class? You're looking at it backwards. You should extend Exception when you want to add functionality to Extension. If you make a custom SQL_Exception class, it should extend Extension, not whatever class you're using to represent your database. The database class can then throw your new SQL_Exception, which can be caught later down the line. A database, a form, an autoloader... none of these are exceptions in and of themselves, nor should they be. The most they should do is throw custom exceptions that relate specifically to them. Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206420 Share on other sites More sharing options...
Destramic Posted April 26, 2011 Author Share Posted April 26, 2011 well thats where i was gonna have Autoloader_Exceptions extents Exception { } Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206441 Share on other sites More sharing options...
KevinM1 Posted April 26, 2011 Share Posted April 26, 2011 well thats where i was gonna have Autoloader_Exceptions extents Exception { } So, why is your autoloader extending that? You don't need inheritance if you simply want to use another object. Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206468 Share on other sites More sharing options...
Destramic Posted April 26, 2011 Author Share Posted April 26, 2011 ok so scrap the idea and just leave the class as it is without extending a exception handler Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206478 Share on other sites More sharing options...
KevinM1 Posted April 26, 2011 Share Posted April 26, 2011 Just to be absolutely clear, something like (pseudo-code): class Autoloader_Exception extends Exception{} class Autoloader { public someFunction() { if (/* some bad condition */) { throw new Autoloader_Exception(); } else { // continue } } } Is just fine. Also, Exception isn't an exception handler. Exception simply encapsulates error data. They're handled in try-catch blocks. Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206484 Share on other sites More sharing options...
Destramic Posted April 26, 2011 Author Share Posted April 26, 2011 Nightslyr thanks of the example i think thats what i need... just to use instead of throw new Autoloader_Exception throw new Exception Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206509 Share on other sites More sharing options...
KevinM1 Posted April 26, 2011 Share Posted April 26, 2011 Nightslyr thanks of the example i think thats what i need... just to use instead of throw new Autoloader_Exception throw new Exception Yeah, that would work. However, just to be clear, it makes sense to subclass Exception if you need to track what kind of exception is being thrown, or if certain exceptions need more information to be recorded. Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206512 Share on other sites More sharing options...
Destramic Posted April 27, 2011 Author Share Posted April 27, 2011 well thanks guys you've been very helpful...and ive finnished my autoloader class if you want to have a look basically it has a function which will read all files in the directory ending with .class.php and store the path so it is ready to be called <?php class Autoloader { private $_class_paths = array(); private $_ignore_directories = array(); private $_declared_classes = array(); public function __construct($config) { $this->_ignore_directories = $config->autoloader_ignore_directories; $this->get_class_paths(); spl_autoload_register(array($this, 'load_class')); } private function load_class($file) { $file = strtolower($file); $path = $this->_class_paths[$file]; try { if (!class_exists($file, FALSE)) { if (file_exists($path)) { require_once $path; $this->_declared_classes[] = $file; } else { throw new Autoloader_Exception(sprintf("Class '%s' not found.<br />\n", $file)); } } } catch (Exception $e) { echo $e->getMessage(); } } private function get_class_paths($path = ROOT) { $dh = opendir($path); while (false !== ($file = readdir($dh))) { if (!in_array($file, $this->_ignore_directories)) { if (is_dir($path . DS . $file)) { $this->get_class_paths($path . DS . $file); } else { if (preg_match("/\.class\.php$/i", $file)) { $file = substr($file, 0, -10); $this->_class_paths[$file] = $path . DS . $file . '.class.php'; } } } } closedir($dh); } public function declared_classes() { echo "<pre>"; print_r($this->_declared_classes); echo "</pre>"; } } if you guys can tell me what you think...or if you have any thing you think i should change please...thank you again Quote Link to comment https://forums.phpfreaks.com/topic/234747-autloader-class-extend-problem/#findComment-1206669 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.