- private static $_instance;
Copy Code2), constructors, and clone functions must be declared private to prevent the external program new class from losing the meaning of the singleton pattern:
- Private Function __construct ()
- {
- $this->_db = Pg_connect (' xxxx ');
- }
- Private Function __clone ()
- {
- }//overwrite __clone () method, prohibit cloning
Copy Code(3). You must provide a public static method that accesses this instance (typically the GetInstance method), which returns a reference to the unique instance
- public static function getinstance ()
- {
- if (! (Self::$_instance instanceof Self))
- {
- Self::$_instance = new self ();
- }
- return self::$_instance;
- }
Copy CodeSecond, why use the PHP design mode of the singleton mode? 1, PHP Disadvantage: PHP language is an explanatory type of scripting language, this operating mechanism so that each PHP page is interpreted to execute, all the relevant resources will be recycled. In other words, PHP does not have a language level to allow an object to reside in memory, which is different from the compilation of ASP. NET, Java, such as the single-instance in Java has been in the entire application life cycle, variables are cross-page level, It is true that this instance is unique in the application life cycle. However, in PHP, all variables, whether global variables or static members of the class, are page-level, each time the page is executed, the new object will be re-created, will be emptied after the page executes, so it seems that PHP singleton mode is meaningless, So PHP singleton mode I think it makes sense to just have multiple scenarios for a single page-level request and need to share the same object resource. 2, Singleton mode in PHP applications : 1), Application and database interaction there will be a large number of database operations in an application, such as the database handle to connect the database behavior, using singleton mode can avoid a large number of new operations, Because each new operation consumes memory resources and system resources. 2), control configuration information if a class is needed in the system to control some configuration information globally, it can be easily implemented using singleton mode. third, how to achieve a single case mode? 1. Common Database Access Examples:
- ......
- Initialize a database handle
- $db = new db (...);
- Add user Information
- $db->adduserinfo (...);
- ......
- Accessing the database in a function to find user information
- function GetUserInfo ()
- {
- $db = new db (...); /again new database class, and database establish connection
- $db = Query (...); /database access based on query statement
- }
- ?>
Copy Code2, apply the singleton mode to the database operation:
- Class DB
- {
- Private $_db;
- private static $_instance;
- Private function __construct (...)
- {
- $this->_db = Pg_connect (...); /postgrsql
- }
- Private Function __clone () {}; Overwrite __clone () method, prohibit cloning
- public static function getinstance ()
- {
- if (! (Self::$_instance instanceof Self)) {
- Self::$_instance = new self ();
- }
- return self::$_instance;
- }
- Public Function Adduserinfo (...)
- {
- }
- Public function GetUserInfo (...)
- {
- }
- }
- Test
- $db = Db::getinstance ();
- $db->adduserinfo (...);
- $db->getuserinfo (...);
- ?>
Copy Code3. In-depth understanding
- /**
- Database Operations Classes
- @link http://bbs.it-home.org
- */
- Class DB {
- public $conn;
- public static $sql;
- public static $instance =null;
- Private Function __construct () {
- Require_once (' db.config.php ');
- $this->conn = mysql_connect ($db [' Host '], $db [' User '], $db [' Password '];
- if (!mysql_select_db ($db [' database '], $this->conn)) {
- echo "Failure";
- };
- mysql_query (' Set names UTF8 ', $this->conn);
- }
- public static function getinstance () {
- if (Is_null (self:: $instance)) {
- Self:: $instance = new db;
- }
- Return self:: $instance;
- }
- /**
- * Query Database
- */
- Public Function Select ($table, $condition =array (), $field = Array ()) {
- $where = ";
- if (!emptyempty ($condition)) {
- foreach ($condition as $k = = $v) {
- $where. = $k. " = ' ". $v." ' and ";
- }
- $where = ' where '. $where. ' 1=1 ';
- }
- $fieldstr = ";
- if (!emptyempty ($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 Function Insert ($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 Function Update ($table, $data, $condition =array ()) {
- $where = ";
- if (!emptyempty ($condition)) {
- foreach ($condition as $k = = $v) {
- $where. = $k. " = ' ". $v." ' and ";
- }
- $where = ' where '. $where. ' 1=1 ';
- }
- $updatastr = ";
- if (!emptyempty ($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 Function Delete ($table, $condition) {
- $where = ";
- if (!emptyempty ($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 function Getlastsql () {
- echo self:: $sql;
- }
- }
- $db = Db::getinstance ();
- $list = $db->select (' demo ', Array (' name ' = ' tom ', ' password ' = ' ds '), Array (' name ', ' password '));
- echo $db->insert (' demo ', Array (' name ' = ' recent you ', ' password ' = ' 123 '));
- echo $db->update (' demo ', Array ("name" = ' xxx ', "password" = ' 123 '), array (' ID ' =>1));
- echo $db->delete (' demo ', Array (' id ' = ' 2 '));
- Db::getlastsql ();
- echo "
";
Copy Code |