Guest User

Untitled

a guest
May 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. <?php
  2. class ImplementationError extends Exception {
  3. public function __construct() {}
  4. }
  5.  
  6. class DatabaseException extends Exception {
  7. public $id;
  8.  
  9. public function __construct($id, $message) {
  10. $this->id = $id;
  11. $this->message = $message;
  12. }
  13. }
  14.  
  15. /* Static connection handler, which returns a Database */
  16. class ConnectionHandler {
  17. private static $databases = array();
  18.  
  19. public static function instance($id, $dsn = null, $type = 'Database') {
  20. /* If $dsn is not null (we being told to connect to the database) */
  21. if ($dsn !== null) {
  22. self::$databases[$id] = new $type($dsn);
  23. }
  24.  
  25. if (self::$databases[$id] instanceof Database) {
  26. return & self::$databases[$id];
  27. } else {
  28. throw DatabaseException($id, 'No connection to the database');
  29. }
  30. }
  31. }
  32.  
  33. class Database {
  34. protected $connection;
  35. public function __construct($dsn) {
  36. throw ImplmentationError;
  37. }
  38.  
  39. public function query($sql) {
  40. throw ImplmentationError;
  41. }
  42.  
  43. public function select($what, $from, $where = null) {
  44. $sql = 'SELECT ' . $what . ' FROM ' . $from;
  45.  
  46. if ($where !== null) {
  47. $sql .= ' WHERE ' . $where;
  48. }
  49.  
  50. return $this->query($sql);
  51. }
  52. }
  53.  
  54. function my_default_exception_handler($exception) {
  55. if ($exception instanceof DatabaseException) {
  56. if ($e->id === 'default') {
  57. // The exception was with the default database.
  58. echo 'Database problem: ' . $exception->getMessage();
  59. exit();
  60. } else {
  61. // The exception was with another database, if this was not an issue, then the
  62. // "my_default_exception_handler" exception handler would of never got the
  63. // exception. If it should be checked, it should of been caught by when it was ran.
  64. return;
  65. }
  66. } elseif ($exception instanceof ImplmentationError) {
  67. echo 'ImplmentationError on line: ' . $exception->getLine() . ' in ' . $exception->getFile() . '<br />';
  68. return;
  69. } else {
  70. echo 'Exception: ' . $exception->getMessage();
  71. exit();
  72. }
  73. }
  74. set_exception_handler('my_default_exception_handler');
  75.  
  76. class MySQLDatabase extends Database {
  77. public function __construct($dsn) {
  78. $server = explode(';', $dsn);
  79. $this->connection = mysql_connect($server[0], $server[1], $server[2]);
  80. }
  81.  
  82. public function query($sql) {
  83. return mysql_query($sql, $this->connection);
  84. }
  85. }
  86.  
  87.  
  88. $default = Database::instance('default', 'localhost:username;password', 'MySQLDatabase');
  89. $data = $default->query('SELECT * FROM users');
  90.  
  91. // Somewhere else:
  92. $backup = Database::instance('backup');
  93. /* This would throw an error, because we have not opened a connection to our backup database yet.
  94. Better to use the following, as this checks: */
  95. try {
  96. $backup = Database::instance('backup');
  97. } catch (DatabaseException $e) {
  98. // If DatabaseException is throw, that means no connection to database, lets make one
  99. $backup = Database::instance('backup', 'settings_for_backup', 'MySQLDatabase');
  100. }
  101. ?>
Add Comment
Please, Sign In to add comment