voici la Classe en question
<?php
class DatabaseConnection
{
private static $_instance = null;
private $_host;
private $_user;
private $_password;
private $_dbname;
private $_handle;
private function __construct($dbname = 'nightkid')
{
$this->_host = 'localhost';
$this->_user = 'root';
$this->_password = '';
$this->_dbname = $dbname;
$this->_handle = null;
try {
$this->_handle = new PDO("mysql:host=$this->_host;dbname=$this->_dbname", $this->_user, $this->_password);
$this->_handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connection established and database "' . $this->_dbname . '" selected.';
} catch (PDOException $e) {
die('Connection failed or database cannot be selected : ' . $e->getMessage());
}
}
public function __destruct()
{
var_dump($this->_db); // Vérification
if (!is_null($this->_handle)) {
$this->_handle = null;
echo 'Connection closed.';
}
var_dump($this->_db); // Une dernière vérification pour voir si l'objet a été détruit
}
public static function getInstance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
public function handle()
{
return $this->_handle;
}
}
Maintenant, par exemple, j'utilise cette Classe comme suit :<?php
require_once 'DatabaseConnection.php';
class Albums
{
private $_db;
private $_sql;
private $_statement;
public function __construct()
{
$this->_db = DatabaseConnection::getInstance()->handle();
}
public function __destruct()
{
var_dump($this->_db); // Vérification
}
public function addAlbum($artist, $title)
{
try {
$this->_sql = "INSERT INTO albums (artist, title) VALUES (:artist, :title)";
$this->_statement = $this->_db->prepare($this->_sql);
$this->_statement->bindParam(':artist', $artist);
$this->_statement->bindParam(':title', $title);
$this->_statement->execute();
echo 'Album added.';
} catch (PDOException $e) {
die('Error->addAlbum() : ' . $e->getMessage());
}
}
public function editAlbum($id, $artist, $title)
{
try {
$this->_sql = "UPDATE albums SET artist = :artist, title = :title WHERE id = :id";
$this->_statement = $this->_db->prepare($this->_sql);
$this->_statement->bindParam(':id', $id);
$this->_statement->bindParam(':artist', $artist);
$this->_statement->bindParam(':title', $title);
$this->_statement->execute();
echo 'Album information updated.';
} catch (PDOException $e) {
die('Error->editAlbum() : ' . $e->getMessage());
}
}
public function deleteAlbum($id)
{
try {
$this->_sql = "DELETE FROM albums WHERE id = :id";
$this->_statement = $this->_db->prepare($this->_sql);
$this->_statement->bindParam(':id', $id);
$this->_statement->execute();
echo 'Album deleted.';
} catch (PDOException $e) {
die('Error->deleteAlbum() : ' . $e->getMessage());
}
}
public function selectAlbum($id = null)
{
try {
if ($id !== null && $id > 0) {
$this->_sql = "SELECT id, artist, title FROM albums WHERE id = :id";
$this->_statement = $this->_db->prepare($this->_sql);
$this->_statement->bindParam(':id', $id);
} else {
$this->_sql = "SELECT id, artist, title FROM albums";
$this->_statement = $this->_db->prepare($this->_sql);
}
$this->_statement->setFetchMode(PDO::FETCH_ASSOC);
$this->_statement->execute();
return $this->_statement->fetchAll();
} catch (PDOException $e) {
die('Error->selectAlbum() : ' . $e->getMessage());
}
}
}
PS: J'aimerai aussi savoir si l'utilisation de la méthode magique "__destruct" dans la Classe "DatabaseConnection" est utile dans ce cas là ou pas.