Destramic Posted February 11, 2011 Share Posted February 11, 2011 hey guys i have a static attribute $_exception_handler which is set to Exception and....now this value could change to a customer exception_handler but what i want to do is <?php catch (self::$_exception_handler $e) { } but im getting an error...is there a way of doing this...any help would be greatful thanks class below <?php class Autoloader { protected static $_exception_handler = Exception; protected static $_classes = array(); public static function load_library($class_name) { $file = ROOT . DS. LIBRARY_DIRECTORY . DS . $class_name . CLASS_EXTENSION; try { self::load_class($class_name, $file); } catch (self::$_exception_handler $e) { echo $e->getMessage(); } } public static function load_exception($class_name) { } public static function load_class($class_name, $file) { if (!class_exists($class_name, FALSE)) { if (file_exists($file)) { require_once $file; } else { throw new Exception(sprintf('Class %s not found.', $class_name)); } } } } Quote Link to comment https://forums.phpfreaks.com/topic/227408-oop-static-vars-help/ Share on other sites More sharing options...
KevinM1 Posted February 11, 2011 Share Posted February 11, 2011 Why not make a custom Exception object which inherits from Exception? Instead of storing a custom Exception within your static class (which isn't doing what you hope to do), simply throw your custom Exception when necessary and catch it like normal. In other words: Custom_Exception extends Exception { // custom properties and methods } class Autoloader { protected static $classes = array(); public static function load_library($class_name) { // ... try { self::load_class($class_name, $file); } catch(Custom_Exception $e) { echo $e->message; } } } This will allow you to catch various kinds of Exceptions, so if something doesn't fit your custom criteria, you could still handle more generic errors. Quote Link to comment https://forums.phpfreaks.com/topic/227408-oop-static-vars-help/#findComment-1173013 Share on other sites More sharing options...
Destramic Posted February 11, 2011 Author Share Posted February 11, 2011 thats where my Autoloader_Exception class is gonna come in...but if that doesn't load (if file doesn't exsits) then i want the handler to be Exception... is there a way of getting this to work? catch (self::$_exception_handler $e) { } thank you Quote Link to comment https://forums.phpfreaks.com/topic/227408-oop-static-vars-help/#findComment-1173019 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.