Bonjour à tous
pour faire simple, j'essaie de faire un singleton sur un object PDO.
Voici la classe de cette objet
Par contre après, quand je lance ce code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 class instanceDB { private static $instance = NULL; private $db; /** * Constructor */ function __construct(){ try { $this->db = new PDO('mysql:host=host;dbname=db', 'login', 'pwd'); } catch(PDOException $e) { print "Erreur PDO ! : " . $e->getMessage() . "<br/>"; die(); } } public static function getInstance() { if( !(self::$instance) ) { self::$instance = new instanceDB(); } return self::$instance; } function getDB(){ return $this->db; } }
J'obtiens
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 require_once 'instanceDB.php'; class table { private $insert; public function __construct() { $instanceDB = instanceDB::getInstance(); var_dump($instanceDB->getDB()); $this->insert = $instanceDB->getDB()->prepare('INSERT INTO table (id) VALUES 0'); } public function insert() { $this->insert->execute(); return $this->insert->rowCount(); } }
Lorsque j'instancie une fois mon object PDO mais sans classe singleton, le var_dump() retourne la même chose et tout fonctionne!object(PDO)#2 (0) { }
Fatal error: Call to a member function prepare() on a non-object in ..table.php
Des idées?
Merci d'avance!
Partager