HP Singleton mode (multiple connection databases are instantiated only once)

Source: Internet
Author: User
Tags constructor

I used to connect to the database when I first started working, and I used the new instance to connect with the database every time I used it, because the number of connections to the database was not very frequent, so it was nothing. Later, the supervisor told me that I am now connected to the database every time if the data read frequently, the pressure on the database and the system will be very large, let me think of a way to connect a database and then use it again when you do not have a new connection, then how did not think of a good method, Know that recently learned a single model to realize, then the supervisor is to guide me to use a singleton mode, only blame me before the development mode does not understand. Okay, cut the crap, here's a list of singleton patterns:

Singleton mode (duty mode):

Simply put, an object (before learning design patterns, need to understand the object-oriented thinking) is only responsible for a specific task;

Singleton class:

1. The constructor needs to be marked as private (access control: Prevent external code from creating objects using the new operator), the Singleton class cannot be instantiated in other classes, and can only be instantiated by itself;

2. Have a static member variable that holds an instance of the class

3, has a public access to this instance of the static method (common getinstance () method to instantiate a singleton class, through the instanceof operator can detect whether the class has been instantiated)

In addition, you need to create a __clone () method to prevent the object from being copied (cloned)

Why use PHP singleton mode.

1, the application of PHP 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.

2, if the system needs to have a class to control some of the configuration information globally, then using the singleton mode can be easily implemented. This can be see the frontcontroller part of ZF.

3, in a page request, easy to debug, because all the code (such as database Operation class DB) is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump everywhere, echo.

Code (in the official document):


Class singletons{

Save the class instance in this property

private static $instance;

The construction method is declared private, preventing the object from being created directly

Private Function __construct ()


Echo ' Iam constructed ';


Singleton method

public static function Singleton ()


if (!isset (self:: $instance)) {

$c = __class__;

Self:: $instance =new$c;


Return self:: $instance;


Common methods in the example class

Public Function bark ()


Echo ' woof! ';


Prevent users from replicating object instances

Public Function __clone ()


Trigger_error (' Clone is not allowed. ', e_user_error);


}//endclass singletons

This is an error because the constructor method is declared as private.

$test = new singletons ();

The following will get the singleton object of the example class

$test = Singletons::singleton ();

$test->bark ();

Copying an object will result in a e_user_error.

$test _clone= clone $test;



I am constructed! woof!

Fatal Error:

Clone is not allowed. In E:\APMServ5.2.6\www\htdocs\Lee\myprogram\other\class\singletons.phpon Line 31

The following is from the network (you can learn it by reference):

Three points of the singleton pattern:

(1). You need a static member variable that holds a unique instance of the class:

[PHP] View plaincopyprint?

1. private static $_instance;

(2). Constructors and clone functions must be declared private to prevent the external program new class from losing the meaning of the singleton pattern:

[PHP] View plaincopyprint?

1. Private Function __construct ()

2. {

3. $this->_db = Pg_connect (' xxxx ');


5. Private Function __clone ()

6. {

7.}//cover __clone () method, prohibit cloning


(3). You must provide a public static method that accesses this instance (typically the GetInstance method), which returns a reference to the unique instance

[PHP] View plaincopyprint?

1. public static function getinstance ()

2. {

3. if (! (Self::$_instance instanceof Self))

4. {

5. Self::$_instance =

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.