PHP Singleton development mode (responsibility mode)

Source: Internet
Author: User
Unlike normal classes, a singleton class cannot be directly instantiated in other classes. A singleton class can only be instantiated by itself. to obtain this restriction, you must mark the constructor as private. To make the singleton class take effect, you must provide an instance for other classes and use it to call various methods. As a model

Unlike normal classes, a singleton class cannot be directly instantiated in other classes. A singleton class can only be instantiated by itself. to obtain this restriction, you must mark the constructor as private. To make the singleton class take effect, you must provide an instance for other classes and use it to call various methods. As part of this mode, you must create an empty private _ clone () method to prevent objects from being copied and cloned. The Singleton is summarized as follows:
1. they must have a constructor marked as private.
2. they have a static member variable that saves the class instance.
3. they have a public static method to access this instance. The instance code is as follows:

Flat view printing?
01 ClassDB
02 {
03 Private $ _ db;
04
05 // Store the static variables of an instance
06 Privatestatic $ _ instance;
07
08 // Construct a private method
09 Privatefunction _ construct ()
10 {
11 $ This-> _ db = mysql_connect ('localhost', 'root', 'admin ');
12 }
13 // Prevent the object from being cloned
14 Privatefunction _ clone ()
15 {
16 }
17 PublicstaticfunctiongetInstance ()
18 {
19 // Prevent multiple instances
20 If (! (Self: $ _ instanceinstanceof self ))
21 {
22 Self: $ _ instance = newself ();
23 }
24 Returnself: $ _ instance;
25 }
26 // Other methods
27 Publicfunctionquery ()
28 {
29 // Code goes here
30 Return 'query ';
31 }
32 PublicfunctionselectDB ()
33 {
34 // Code goes here
35 Return 'selectdb ';
36 }
37 }
38
39 // Obtain the instance of the class
40 $ Object = DB: getInstance ();
41
42 // Access the method
43 Echo $ object-> query ();


This class begins to declare two variables, one is the private variable $ _ db storage database connection, will be filled during the construction, and the other is used for storage class instances. The next step is private constructor and magic function clone, which can prevent classes from being instantiated and cloned externally. the getInstance static method is used for the worker class instance. the code here can ensure the existence of a single instance, reduce system resource consumption and return instance references.


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.