Created
July 10, 2025 05:58
-
-
Save moriarty99779/3d47c48f9a5f09c3905e766d7d7cb460 to your computer and use it in GitHub Desktop.
PHP 8 Singleton PostgreSQL Database Connection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Database { | |
private static ?Database $instance = null; | |
private ?resource $connection = null; | |
private string $host; | |
private string $dbname; | |
private string $user; | |
private string $password; | |
// Private constructor to prevent instantiation | |
private function __construct(string $host, string $dbname, string $user, string $password) { | |
$this->host = $host; | |
$this->dbname = $dbname; | |
$this->user = $user; | |
$this->password = $password; | |
$this->connect(); | |
} | |
// Private clone method to prevent cloning | |
private function __clone() {} | |
// Private unserialize method to prevent unserialization | |
private function __wakeup() {} | |
// Create the connection to the PostgreSQL database | |
private function connect(): void { | |
$connectionString = "host={$this->host} dbname={$this->dbname} user={$this->user} password={$this->password}"; | |
$this->connection = pg_connect($connectionString); | |
if (!$this->connection) { | |
throw new Exception("Connection failed: " . pg_last_error()); | |
} | |
} | |
// Get the instance of the Database Singleton | |
public static function getInstance(string $host, string $dbname, string $user, string $password): Database { | |
if (self::$instance === null) { | |
self::$instance = new Database($host, $dbname, $user, $password); | |
} | |
return self::$instance; | |
} | |
// Return the current database connection | |
public function getConnection(): ?resource { | |
return $this->connection; | |
} | |
// Close the connection | |
public function closeConnection(): void { | |
if ($this->connection) { | |
pg_close($this->connection); | |
$this->connection = null; | |
} | |
} | |
} | |
// Example usage code follows... | |
// Set your PostgreSQL database credentials | |
$host = 'localhost'; | |
$dbname = 'your_database'; | |
$user = 'your_user'; | |
$password = 'your_password'; | |
try { | |
// Get the Singleton instance and get the connection | |
$db = Database::getInstance($host, $dbname, $user, $password); | |
$connection = $db->getConnection(); | |
// Use the connection to execute queries | |
$result = pg_query($connection, "SELECT * FROM users"); | |
if ($result) { | |
while ($row = pg_fetch_assoc($result)) { | |
echo "ID: " . $row['id'] . " - Name: " . $row['name'] . "<br>"; | |
} | |
} else { | |
echo "Error: " . pg_last_error($connection); | |
} | |
// Optionally close the connection | |
$db->closeConnection(); | |
} catch (Exception $e) { | |
echo "Error: " . $e->getMessage(); | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment