Single-Case mode
The so-called Singleton pattern, in short, is that a class has only one instance of the runtime and is able to instantiate itself and provide this instance for the entire system to run.
Using singleton mode to instantiate a database connection object in a database connection is primarily a waste of resources that can be avoided by duplicating instantiated objects.
PHP Implementation database Connection object Singleton mode mainly includes three points:
1 requires a unique instance of a saved class static member variable, usually $_instance private variable;
Private $_instance;
2 constructors and clone functions must be declared private to prevent the class from being instantiated externally;
Private Function __construct{
}
Private Function __clone () {
}
3 You must provide a public static method that accesses this instance, typically the GetInstance method, and returns a reference to an instance;
static public Function getinstance () {
if (Is_null (self::$_instance) | |!isset (self::$_instance)) {
Self::$_instance = new DB ();
}
return self::$_instance;
}
*/
PHP application is mainly in the database application, so there will be a large number of database operations in an application, using a singleton mode, you can avoid a large number of new operations to consume resources.
<?PHP/** * $_instance must be declared as a static private variable * Constructors and destructors must be declared private, preventing external programs from new* classes thereby losing the meaning of singleton patterns * The getinstance () method must be set to public and must be called this method * To return a reference to the instance *:: operator can only access static variables and static functions * The new object consumes memory * Usage scenarios: The most common place is the database connection. * Once an object is generated using singleton mode, the object can be used by many other objects. **/$db=Array( ' Host ' = ' localhost ', ' user ' = ' root ', ' password ' + ', ' ' database ' = ' test ', ); classdb {Private $conn; Public Static $sql; Private Static $instance; Private function__construct () {$this->conn =mysql_connect($db[' Host '],$db[' User '],$db[' Password ']); if(!mysql_select_db($db[' Database '],$this-conn)) { EchoFailed; }; mysql_query(' Set names UTF8 ',$this-conn); } //To Create a __clone method to prevent an object from being cloned Private function__clone () {//Trigger_error (' Clone is not allow! ', e_user_error); } //A singleton method, a public static method for accessing an instance Public Static functiongetinstance () {if(Is_null(Self::$instance)){// ! (Self::$_instance instanceof Self)Self::$instance=NewDB; } returnSelf::$instance; } /** * Query Database*/ Public functionSelect$table,$condition=Array(),$field=Array()){ $where= ' '; if(!Empty($condition)){ foreach($condition as $k=$v){ $where.=$k." = ' ".$v. "' and"; } $where= ' where '.$where.‘ 1=1 '; } $fieldstr= ' '; if(!Empty($field)){ foreach($field as $k=$v){ $fieldstr.=$v.‘,‘; } $fieldstr=RTrim($fieldstr,‘,‘); }Else{ $fieldstr= ' * '; } Self::$sql= "SELECT {$fieldstr} from {$table} {$where}"; $result=mysql_query(Self::$sql,$this-conn); $resuleRow=Array(); $i= 0; while($row=Mysql_fetch_assoc($result)){ foreach($row as $k=$v){ $resuleRow[$i][$k] =$v; } $i++; } return $resuleRow; } /** * Add a record*/ Public functionInsert$table,$data){ $values= ' '; $datas= ' '; foreach($data as $k=$v){ $values.=$k.‘,‘; $datas. = "'$v‘".‘,‘; } $values=RTrim($values,‘,‘); $datas=RTrim($datas,‘,‘); Self::$sql= "INSERT into {$table} ({$values}) VALUES ({$datas})"; if(mysql_query(Self::$sql)){ return mysql_insert_id(); }Else{ return false; }; } /** * Modify a record*/ Public functionUpdate$table,$data,$condition=Array()){ $where= ' '; if(!Empty($condition)){ foreach($condition as $k=$v){ $where.=$k." = ' ".$v. "' and"; } $where= ' where '.$where.‘ 1=1 '; } $updatastr= ' '; if(!Empty($data)){ foreach($data as $k=$v){ $updatastr.=$k." = ' ".$v."‘,"; } $updatastr= ' Set '.RTrim($updatastr,‘,‘); } Self::$sql= "Update {$table} {$updatastr} {$where}"; return mysql_query(Self::$sql); } /** * Delete records*/ Public functionDelete$table,$condition){ $where= ' '; if(!Empty($condition)){ foreach($condition as $k=$v){ $where.=$k." = ' ".$v. "' and"; } $where= ' where '.$where.‘ 1=1 '; } Self::$sql= "Delete from {$table} {$where}"; return mysql_query(Self::$sql); } Public Static functionGetlastsql () {EchoSelf::$sql; } }$db= DB::getinstance (); Echo $db->delete (' demo ',Array(' id ' = ' 2 ')); DB:: Getlastsql ();
PHP Single-instance mode