random1 Posted March 8, 2008 Share Posted March 8, 2008 How do you create your own Exception class in PHP? My base programming language is Java, I can do it in that but I'm still pretty new PHP. Can you create you an Exception class that catches it's own exceptions e.g. 'CustomException cex' ? Also how do I make that code available to all other pages and classes in my project? Link to comment https://forums.phpfreaks.com/topic/95041-exception-handling-and-exception-class/ Share on other sites More sharing options...
Daniel0 Posted March 8, 2008 Share Posted March 8, 2008 You just extend the Exception class: <?php class CustomException extends Exception { // custom functionality here } ?> Remember to call parent::__construct() if you override the constructor. Link to comment https://forums.phpfreaks.com/topic/95041-exception-handling-and-exception-class/#findComment-487142 Share on other sites More sharing options...
deadonarrival Posted March 8, 2008 Share Posted March 8, 2008 And parent::__destruct if you override the destructor You don't need to REPLACE the exception class - just extend it. Don't re-make it's existing methods unless you need to do something totally different. If it's only a little different, you can probably still use the existing method within one of your own Link to comment https://forums.phpfreaks.com/topic/95041-exception-handling-and-exception-class/#findComment-487171 Share on other sites More sharing options...
Daniel0 Posted March 9, 2008 Share Posted March 9, 2008 And parent::__destruct if you override the destructor Exception does not have a destructor... Link to comment https://forums.phpfreaks.com/topic/95041-exception-handling-and-exception-class/#findComment-487554 Share on other sites More sharing options...
deadonarrival Posted March 9, 2008 Share Posted March 9, 2008 Whoops fair enough. Link to comment https://forums.phpfreaks.com/topic/95041-exception-handling-and-exception-class/#findComment-487913 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.