Guest User

Untitled

a guest
Mar 6th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. <?php
  2. class DbConnector {
  3. private $_connection;
  4. private static $_instance;
  5. private $_host = "host";
  6. private $_username = "username";
  7. private $_password = "password";
  8. private $_database = "dbname";
  9. private $_dbport = 3306;
  10.  
  11. public static function getInstance() {
  12. if(!self::$_instance) {
  13. self::$_instance = new self();
  14. }
  15. return self::$_instance;
  16. }
  17. private function __construct() {
  18. $this->_connection = new mysqli($this->_host, $this->_username,
  19. $this->_password, $this->_database, $this->_dbport);
  20. if(mysqli_connect_error()) {
  21. trigger_error("Fatal MySQL Connection Error " . mysql_connect_error(),
  22. E_USER_ERROR);
  23. }
  24. }
  25.  
  26. private function __clone() { }
  27. public function getConnection() {
  28. return $this->_connection;
  29. }
  30. }
  31.  
  32. class SomeOtherClass
  33. {
  34. $conn = DbConnector::getInstance();
  35. $db = $conn->getConnection();
  36. # use $db->*(*)......
  37. }
  38. ?>
Add Comment
Please, Sign In to add comment