PHP Single Example mode detailed introduction

Source: Internet
Author: User

This article mainly introduced the PHP single example mode detailed introduction, this article explained the single example pattern the concept, the single example pattern characteristic, uses the single example pattern the reason and the scene and the PHP single example pattern code example, needs the friend to be possible to refer to under

The concept of a single case pattern

A single example pattern is a design pattern in which a class has only one object instance in the entire application. Specifically, as an object creation, the singleton pattern ensures that a class has only one instance, and instantiates it itself and provides this instance to the entire system globally. Instead of creating an instance copy, it returns a reference to an instance stored within a singleton class.

Characteristics of a single case model

The main feature of the single case model is "three private one":

A private static member variable is required to hold a unique instance of the class

Constructors must be declared private to prevent an external program from creating an object that loses the meaning of a single instance

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

A public static method (usually named getinstance) that accesses this instance must be provided to return a reference to a unique instance.

Reasons and scenarios for using a single case pattern

In most PHP applications there will be a large number of database operations, if you do not use the single example mode, that each time the new operation, but each time new will consume a lot of system resources and memory resources, and each open and close the database is a great test of the database and waste. Therefore, a single example pattern is often used in database operations classes.

Similarly, if you need a class in your system to control some configuration information globally, then using a single example pattern can be a convenient implementation.

PHP Single Example mode implementation

Here is a PHP single example schema to implement the database Operation class framework

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

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.