"PHP" Design database connection model class with single case mode

Source: Internet
Author: User

The MySQL database query class previously written in "PHP" using PHP's constructors and destructors to write MySQL database queries is not perfect, the idea of using the "Java" single example mode (click Open link) to make a single example of this database link class Not because multiple users visit the site to create a database query instance, slow down the speed of the entire site, so that the site's database pressure is relatively large, causing the site's speed down very seriously.

Single example to achieve the most critical, or the 3 points:

1. Private constructors, where you don't need to have a private parameterless constructor like Java, PHP does not allow multiple constructors--even if these constructor parameters are different.

2. Function of Private Clone class

3. Exposing a public "create instance function" for invocation, this "create instance function" determines that if a corresponding instance already exists, the instance is returned without being created.

This guarantees that the database connection class has and is only one.

As a direct example, there is a table user in database test


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


The specific code is as follows:

<?php class db{Private $link;
	DB Class Singleton start//Save class instance's private static member variable private $_instance; Defines a private constructor to ensure that a singleton class cannot be instantiated through the New keyword and can only be instantiated by itself 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 a private __clone () method to ensure that a single instance class cannot be replicated or cloned private function __clone () {} public static function getinstance ($host, $username, $p Assword, $database) {//detection 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 the database connection public function closed () {return mysql_close ($this->link); }//Call Single instance class test partial header ("content-type:text/html; Charset=utf-8 "); Set page encoding $dbconnector =db::getinstance ("localhost", "root", "root", "test");//CREATE DATABASE connection class $result = $dbcOnnector->query ("SELECT * from User")//Query database for ($i =0; $row =mysql_fetch_array ($result); $i + +) {//Print query results echo $row [' Id ']. ",". $row [' username ']. ",". $row [' Password ']. "
<br/> "; }?>

This guarantees that the class db corresponds to the instance $dbconnector has and only one, and then one sentence:

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

Still will return to the original already created instance $dbconnector, should say the operation $dbconnector and $dbconnector1 are the same effect, they are the same thing, will not in the server's memory to open up resources to store $dbconnector and $ Dbconnector1, because DB sheets are in order to reduce database pressure.

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.