PHP design mode: Singleton mode

Source: Internet
Author: User
PHP design pattern: some days ago, the singleton pattern began to be truly understood. did you start from the Singleton pattern? of course, I read some documents online, the Singleton mode is easy to understand. let's take a look at the introduction, and then look at the basic code to understand it. the design mode is easy to understand, of course, certain practices are also required to make good use of the project. you can't just understand it, or you can understand it very well. once it comes to real-time operations, you won't be able to talk much about it, come on with the PHP team;

Singleton mode(Responsibility model ):

Simply put, an object (before learning the design model, you need to have a better understanding of object-oriented ideas) is only responsible for a specific task;

Singleton class:

1. the constructor must be marked as private (access control: prevents external code from using the new operator to create objects). a singleton class cannot be instantiated in other classes but can only be instantiated by itself;

2. static member variables of an instance that stores classes

3. there is a public static method to access this instance (the getInstance () method is often used to instantiate the singleton class, and the instanceof operator can detect whether the class has been instantiated)

In addition, you must create the _ clone () method to prevent objects from being copied (cloned)

Why use the PHP Singleton mode?

1. php applications mainly lie in database applications. Therefore, a large number of database operations exist in an application. Using the Singleton mode can avoid the resources consumed by a large number of new operations.

2. if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode. for details, refer to the FrontController section of ZF.

3. in a page request, debugging is easy. because all the code (such as database operation db) is concentrated in a class, we can set hooks in the class and output logs, this avoids var_dump and echo everywhere.

Code implementation:

<111? 111php111

/1 **

* Singleton mode in design mode

* $ _ Instance must be declared as a static private variable

* Constructor and Destructor must be declared as private to prevent external programs from new

* Class, thus losing the significance of Singleton mode

* The getInstance () method must be set to public and must be called

* To return a reference to the instance

*: The operator can only access static variables and static functions.

* New objects consume memory.

* Use cases: database connections are the most common scenarios.

* After an object is generated in Singleton mode,

* This object can be used by many other objects.

*/

Class Danli {

// Save the static member variables of the class instance

Private static $ _ instance;

// Construct a private tag

Private function _ construct (){

Echo 'This is a Constructed method ;';

}

// Create the _ clone method to prevent objects from being copied and cloned.

Public function _ clone (){

Trigger_error ('Clone is not allow! ', E_USER_ERROR );

}

// Singleton method, which is a public static method used to access the instance

Public static function getInstance (){

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

Self: $ _ instance = new self;

}

Return self: $ _ instance;

}

Public function test (){

Echo 'method invoked successfully ';

}

}

// An error will be reported when the class that marks the constructor with the new instantiated private flag

// $ Danli = new Danli ();

// Correct method. use the double colon: Operator to access the static method to obtain the instance

$ Danli = Danli: getInstance ();

$ Danli-> test ();

// Copying (cloning) the object will cause an E_USER_ERROR

$ Danli_clone = clone $ danli;

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.