diff options
Diffstat (limited to 'php/ConnectionDatabase.php')
-rw-r--r-- | php/ConnectionDatabase.php | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/php/ConnectionDatabase.php b/php/ConnectionDatabase.php new file mode 100644 index 0000000..b26d0bd --- /dev/null +++ b/php/ConnectionDatabase.php | |||
@@ -0,0 +1,35 @@ | |||
1 | <?php | ||
2 | // php/ConnectionDatabase.php | ||
3 | |||
4 | class Connection extends PDO | ||
5 | { | ||
6 | // paramètres du constructeur de PDO, avec sqlite seul le premier est nécessaire | ||
7 | public static $dsn = ''; // Data Source Name = 1er paramètre | ||
8 | public static $user = ''; | ||
9 | public static $password = ''; | ||
10 | public static $options = ''; | ||
11 | private static $Instance; | ||
12 | |||
13 | public function __construct() | ||
14 | { | ||
15 | try | ||
16 | { | ||
17 | parent::__construct(self::$dsn); // renseigne la variable $dsn de la classe PDO | ||
18 | $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // $this pour la méthode du parent PDO | ||
19 | } | ||
20 | catch(PDOException $e) | ||
21 | { | ||
22 | die('Erreur : '.$e->getMessage()); | ||
23 | } | ||
24 | } | ||
25 | |||
26 | // créer son objet avec: $bdd = Connection::getInstance(); | ||
27 | public static function getInstance(): self | ||
28 | { | ||
29 | if(self::$Instance === null) | ||
30 | { | ||
31 | self::$Instance = new self(); | ||
32 | } | ||
33 | return self::$Instance; | ||
34 | } | ||
35 | } | ||