Skip to content

Instantly share code, notes, and snippets.

@o
Created November 22, 2010 16:30
Show Gist options
  • Save o/710212 to your computer and use it in GitHub Desktop.
Save o/710212 to your computer and use it in GitHub Desktop.
Singleton PDO Class, needs PHP 5.3
<?php
class PDO_Singleton {
private static $instance;
public static function getInstance() {
if (!(self::$instance instanceof PDO)) {
self::$instance = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', 'root', array(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION));
}
return self::$instance;
}
final public static function __callStatic($method, $arguments) {
return call_user_func_array(array(self::getInstance(), $method), $arguments);
}
final private function __construct() {
}
final private function __clone() {
}
}
try {
$result = PDO_Singleton::query('SELECT email, password FROM users')->fetchAll(PDO::FETCH_OBJ);
} catch (Exception $exc) {
echo $exc->getMessage();
}
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment