Database.class.php 885 B

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