PHP Singleton Schema Design database and connect model class

Source: Internet
Author: User
using the "Java" Singleton mode (click Open link) introduced the idea can be the database link class into a single case, not because multiple users visit the site to create a database query instance, slow down the entire site speed, so that the site's database pressure is larger, resulting in a severe decline in the speed of the site.

The single implementation of the most critical, or that 3 points:

1. Private constructors, where there is no need for a private parameterless constructor like Java, PHP does not allow multiple constructors-even if these constructor parameters are different.

2. Functions of the Private clone class

3, exposing a public "create instance function" for invocation, this "create instance function" to determine if the corresponding instance already exists, return this instance, did not create.

This ensures that the database connection class has only one.

Directly with an example, the database test has the table user


First use a singleton model to design the database connection model class, the contents of this table query to the Web page:


  1. <?php class db{Private $link;      DB Class Singleton start//save private static member variable of class instance private statics $_instance;          Define a private constructor to ensure that the Singleton class cannot be instantiated with the New keyword and can only be instantiated by itself with the private function __construct ($host, $username, $password, $database) {          $this->link=mysql_connect ($host, $username, $password); if (! $this->link) {die ("Connection failed!          ");          } mysql_query ("Set names UTF8;");      mysql_select_db ($database); }//define Private __clone () method to ensure that the Singleton class cannot be copied or cloned private function __clone () {} public static function getinstance ($host, $ Username, $password, $database) {//detect if the class is instantiated if (! (          Self::$_instance instanceof Self) {self::$_instance=new db ($host, $username, $password, $database);      } return self::$_instance;      }//Execute SQL statement public Function query ($query) {return mysql_query ($query, $this->link); }//Close database connection public function close () {return mysql_close ($this->link);     }}//Call the Singleton class to test the partial header ("content-type:text/html; Charset=utf-8 "); Set the page encoding $dbconnector =db::getinstance ("localhost", "root", "root", "test");//CREATE DATABASE connection class $result = $dbconnector Query ("SELECT * from User"),//Querying database for ($i =0; $row =mysql_fetch_array ($result); $i + +) {//Print query results echo $row [' ID ']. ",". $row [' username ']. ",". $row [' Password ']. "  <br/> "; }?>


This ensures that the instance of class db is $dbconnector and only one, with one sentence:

    1. $dbconnector 1=db::getinstance ("localhost", "root", "root", "test");//Create a database connection class


Or will return the original created instance $dbconnector, more should say Operation $dbconnector and $dbconnector1 is the same effect, they are the same thing, not in the server's memory more resources to store $dbconnector and $ Dbconnector1, because DB is a sample, thus reducing the database pressure to achieve the purpose.

Related recommendations:

set The experience of accounting database _php tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.