Advertisement
Guest User

Untitled

a guest
May 4th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. <?php
  2. class Database
  3. {
  4. # Variável que guarda a conexão PDO.
  5. protected static $db;
  6. # Private construct - garante que a classe só possa ser instanciada internamente.
  7. private function __construct()
  8. {
  9. # Informações sobre o banco de dados:
  10. $db_host = "127.0.0.1";
  11. $db_nome = "teste";
  12. $db_usuario = "root";
  13. $db_senha = "usbw";
  14. $db_driver = "mysql";
  15. # Informações sobre o sistema:
  16. $sistema_titulo = "Nome do Sistema";
  17. $sistema_email = "alguem@gmail.com";
  18. try
  19. {
  20. # Atribui o objeto PDO à variável $db.
  21. self::$db = new PDO("$db_driver:host=$db_host; dbname=$db_nome", $db_usuario, $db_senha);
  22. # Garante que o PDO lance exceções durante erros.
  23. self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  24. # Garante que os dados sejam armazenados com codificação UFT-8.
  25. self::$db->exec('SET NAMES utf8');
  26. }
  27. catch (PDOException $e)
  28. {
  29.  
  30. $e->getMessage();
  31. }
  32. }
  33. # Método estático - acessível sem instanciação.
  34. public static function conexao()
  35. {
  36. # Garante uma única instância. Se não existe uma conexão, criamos uma nova.
  37. if (!self::$db)
  38. {
  39. new Database();
  40. }
  41. # Retorna a conexão.
  42. return self::$db;
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement