class MysqlDB{
static private $instance_MysqlDB = null;
private
$objMysqli;
/**
* Instantiate the object
**/
private function __construct(){
$this->objMysqli = new mysqli("localhost", "root", "", "mysql");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
/**
* Perform a query
*
* @param string $sql
*/
public function select($sql){
$this->objMysqli->query($sql);
$this->var_dumping();
}
/**
* Var dump the current object
*/
public function var_dumping(){
var_dump($this);
}
/**
* Get the current instance for the object
*
* @return object
*/
static public function getInstance(){
if(self::$instance_MysqlDB == null){
self::$instance_MysqlDB = new self;
}
return self::$instance_MysqlDB;
}
}
---------------------------------------
To use the singleton we will never use the constructor method. We will call the getInstance method.Here is an example :
include_once("MysqlDb.php");
MysqlDB::getInstance()->select("SELECT * FROM `help_category` LIMIT 5");
I am a 
0 comments:
Post a Comment