Created
November 22, 2010 16:30
-
-
Save o/710212 to your computer and use it in GitHub Desktop.
Singleton PDO Class, needs PHP 5.3
This file contains 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 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