06-22-2010, 07:51 AM
|
#20 (permalink)
|
|
The Visitor
Join Date: Jun 2010
Location: Hermosillo
Posts: 2
Thanks: 0
|
I have a another example of signletone in php for a connection to a database using myqli for anyone who likes use this.
Code:
class DataBaseConnection {
static private $instance = NULL;
private $objMySqli;
private function __construct() {
$this->objMySqli=new mysqli("host","user","passwd","database_name");
}
static public function getInstance() {
if (self::$instance == NULL) self::$instance = new self;
return self::$instance;
}
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function getMySqli(){
return $this->objMySqli;
}
}
//Usage
$connection= DataBaseConnection::getInstance();//getting the instance forom of the class DataBaseConnection
$result = $connection->getMySqli()->query("select * from tablename");
while($row=$result->fetch_row()){
//print the data of the $row variable
}
$connection2 = new DataBaseConnection()//this will produce an error
|
|
|