In this example, we are creating a MySQLi object and forcing it to a single instance. We just need to call DB::get() to create and/or access the object.
Code: Select all
<?php
class DB
{
private static $instance; // stores the MySQLi instance
private function __construct() { } // block directly instantiating
private function __clone() { } // block cloning of the object
public static function get() {
// create the instance if it does not exist
if(!isset(self::$instance)) {
// the MYSQL_* constants should be set to or
// replaced with your db connection details
self::$instance = new MySQLi(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
if(self::$instance->connect_error) {
throw new Exception('MySQL connection failed: ' . self::$instance->connect_error);
}
}
// return the instance
return self::$instance;
}
}
?>
Code: Select all
$result = DB::get()->query("SELECT * FROM ...");