class DB
{
// singleton instance
private static $instance;
// private constructor function
// to prevent external instantiation
private function __construct()
{
$server = "mysql:dbname=auto;host=127.0.0.1";
$username = "root";
$database_name = "dbname=auto";
$password = "root";
try{
self::$instance = new PDO($server, $username, $password);
}catch(PDOException $e) {
printf("Connect error: %s\n", $e->getMessage());
exit();
}
}
function __destruct()
{
self::$instance = null;
}
// getInstance method
public static function getInstance()
{
if(!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function test()
{
foreach (self::$instance->query("select id from blog") as $row) {
print $row['id'] . "\t";
}
}