Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. <?php
  2. class mysqlDatabase extends dBase { /* ... code here ... */ }
  3. $DB = mysqlDatabase::instance();
  4. $DB->execute("SELECT * FROM foobar WHERE"); // Should throw errors, 'WHERE clause is empty'
  5. // What happens here?
  6.  
  7.  
  8. // Personally, I would do something like this:
  9. class Logger {
  10.     function Log($message) {
  11.         // Log $message
  12.     }
  13. }
  14.  
  15. function defaultExceptionHandler($E) {
  16.     // I would actually probably try to throw up a pretty page, if I had the time
  17.     header('HTTP/1.1 500'); // Set internal service error header
  18.     print "An error has occurred: " . $E->getMessage();
  19.     die();
  20. }
  21.  
  22.  
  23. class DBase {
  24.     function execute($query) {
  25.         try {
  26.             $this->_dbh->exec($query);
  27.         } catch PDOExeption $E {
  28.             Logger::Log($E->getMessage(0);
  29.             throw $E;
  30.         }
  31.     }
  32. }
  33.  
  34.  
  35. // Usage example:
  36. $DB->execute("I am not a valid SQL query");
  37. // What happens now is Logger::Log() got called
  38. // Since I didn't catch the exception, DefaultExceptionHandler() was called which prints a message and dies.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement