PHP Singleton mode-PHP Tutorial

Source: Internet
Author: User
PHP Singleton mode. This article describes the PHP Singleton mode in detail, this article describes the concept of Singleton mode, the characteristics of Singleton mode, the reasons for using Singleton mode, and the detailed description of the scenario PHP Singleton mode.

This article describes the PHP Singleton mode in detail, this article describes the concept of Singleton mode, the characteristics of Singleton mode, the reasons for using Singleton mode, and the scenarios of PHP Singleton mode code instances. For more information, see

Concept of Singleton mode

The Singleton mode refers to the design mode in which a class of the entire application has only one object instance. Specifically, as an object creation method, the singleton mode ensures that a class has only one instance, and instantiate it on its own and provides this instance globally to the entire system. It does not create instance copies, but returns a reference to the instance stored in the singleton class.

Features of Singleton mode

The Singleton mode features "three private and one public ":

You need a private static member variable that stores the unique instance of the class.

The constructor must be declared as private to prevent the external program from losing the meaning of a singleton by creating an object.

The clone function must be declared as private to prevent the object from being cloned.

You must provide a public static method (usually named getInstance) to access this instance, and return a reference to a unique instance.

Causes and scenarios of Singleton mode

In most PHP applications, a large number of database operations exist. if you do not use the Singleton mode, new operations are required each time. However, each new operation consumes a large amount of system resources and memory resources, in addition, each time you open or close the database, it is a great test and waste for the database. Therefore, the singleton mode is often used in database operations.

Similarly, if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode.

PHP Singleton mode implementation

The following is a framework for implementing database operations in the PHP Singleton mode.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

Class Db {

Const DB_HOST = 'localhost ';

Const DB_NAME = '';

Const DB_USER = '';

Const DB_PWD = '';

Private $ _ db;

// Save the private static variable of the instance

Private static $ _ instance;

// Both constructor and clone function are declared as private

Private function _ construct (){

// $ This-> _ db = mysql_connect ();

}

Private function _ clone (){

// Implementation

}

// Public static method for accessing the instance

Public static function getInstance (){

If (! (Self: $ _ instance instanceof self )){

Self: $ _ instance = new self ();

}

// Or

If (self ::$ _ instance === null ){

Self: $ _ instance = new Db ();

}

Return self: $ _ instance;

}

Public function fetchAll (){

// Implementation

}

Public function fetchRow (){

// Implementation

}

}

// Obtain instance reference from outside the class

$ Db = Db: getInstance ();

?>

This article mainly introduces the PHP Singleton mode in detail. This article describes the concept of Singleton mode, the characteristics of Singleton mode, the reasons for using Singleton mode, and the scenarios...

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.