robsonrdasilva Posted June 29, 2011 Share Posted June 29, 2011 Hi folks. I've been studying about exceptions in PHP to improve my code, and I found the Standard PHP Library (SPL). It's a library of functions that help to program. There are many new exceptions, but I didn't find anyone example about it. Do you know use it? Those exceptions are like oldest exceptions? Thanks. Link to comment https://forums.phpfreaks.com/topic/240735-how-do-i-use-the-new-exceptions-of-standard-php-library-spl/ Share on other sites More sharing options...
requinix Posted June 29, 2011 Share Posted June 29, 2011 If you know how to use exceptions then you just... use them. I mean, isn't "OutOfBoundsException" rather self-explanatory? Link to comment https://forums.phpfreaks.com/topic/240735-how-do-i-use-the-new-exceptions-of-standard-php-library-spl/#findComment-1236494 Share on other sites More sharing options...
robsonrdasilva Posted June 29, 2011 Author Share Posted June 29, 2011 So, I may use it to know which type of exception i'm working. Am I right? Link to comment https://forums.phpfreaks.com/topic/240735-how-do-i-use-the-new-exceptions-of-standard-php-library-spl/#findComment-1236515 Share on other sites More sharing options...
requinix Posted June 29, 2011 Share Posted June 29, 2011 Sure. Then you can easily catch specific types. try { doSomething(); } catch (DomainException $de) { // bad input } catch (RangeException $re) { // bad output } catch (Exception $e) { // something else } Essentially, the SPL extension added a few things that the PHP core "should" have had from the start. Link to comment https://forums.phpfreaks.com/topic/240735-how-do-i-use-the-new-exceptions-of-standard-php-library-spl/#findComment-1236542 Share on other sites More sharing options...
robsonrdasilva Posted June 29, 2011 Author Share Posted June 29, 2011 Now I understood =] Thanks Link to comment https://forums.phpfreaks.com/topic/240735-how-do-i-use-the-new-exceptions-of-standard-php-library-spl/#findComment-1236561 Share on other sites More sharing options...
Andy-H Posted June 29, 2011 Share Posted June 29, 2011 Sure. Then you can easily catch specific types. try { doSomething(); } catch (DomainException $de) { // bad input } catch (RangeException $re) { // bad output } catch (Exception $e) { // something else } Essentially, the SPL extension added a few things that the PHP core "should" have had from the start. I like to do it this way: //Abstract/Exception.class.php abstract class Abstract_Exception extends Exception { abstract public function handle(); } //Exception.class.php class fatalError extends Abstract_Exception { public function handle() { trigger_error(stripslashes(htmlentities($this->getMessage()), ENT_QUOTES), E_USER_ERROR); } } class runtimeError extends Abstract_Exception { public function handle() { return '<div class="error">' . stripslashes(htmlentities($this->getMessage()), ENT_QUOTES) . '<>'; } } //functions.php function __autoload($class) { static $includes; if ( !is_array($includes) ) $includes = array(); if ( !in_array($class, $includes) ) { if ( stristr($class, 'Error') ) $class = 'Exception'; $includes[] = $class; $class = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.class.php'; include $class; } } //some script include 'functions.php'; try { //code throw new fatalError('Couldn\'t connect to MySQL database.'); //more code throw new runtimeError('You must enter a username.'); } catch ( Exception $e ) { $error = $e->handle(); } //html echo isset($error) ?: ''; Link to comment https://forums.phpfreaks.com/topic/240735-how-do-i-use-the-new-exceptions-of-standard-php-library-spl/#findComment-1236565 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.