Database.class.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * PDO Connector for database connexion.
  4. *
  5. * @author v.carruesco
  6. *
  7. * @category Core
  8. *
  9. * @license copyright
  10. */
  11. class Database
  12. {
  13. public $connection = null;
  14. public static $instance = null;
  15. private function __construct()
  16. {
  17. $this->connect();
  18. }
  19. /**
  20. * Methode de recuperation unique de l'instance.
  21. *
  22. * @author Valentin CARRUESCO
  23. *
  24. * @category Singleton
  25. *
  26. * @param <Aucun>
  27. *
  28. * @return <pdo> $instance
  29. */
  30. public static function instance()
  31. {
  32. if (self::$instance === null) {
  33. self::$instance = new self();
  34. }
  35. return self::$instance->connection;
  36. }
  37. public function connect()
  38. {
  39. try {
  40. $this->connection = new PDO(BASE_CONNECTION_STRING, BASE_LOGIN, BASE_PASSWORD);
  41. $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  42. } catch (Exception $e) {
  43. echo 'Connection à la base impossible : ', $e->getMessage();
  44. die();
  45. }
  46. }
  47. }