PHP design pattern: Singleton mode

Source: Internet
Author: User

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. Theconstructor 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 if 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 The resource consumed by the new operation.

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 ZF's Frontcontroller part.

3, in a page request , easy to debug , because all the code ( such as database operation Class db) are concentrated in a class , We can set hooks in the class , output the log, thus avoiding var_dump everywhere , Echo.

/1**

* Design pattern of a single case mode

* $_instance must be declared as a static private variable

* Constructors and destructors must be declared private to prevent external program new

* Class to lose the meaning of the singleton pattern

* The getinstance () method must be set to public, this method must be called

* To return a reference to the instance

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

* New objects will consume memory

* Usage Scenario: The most common place is the database connection.

* Once an object is generated using singleton mode,

* This object can be used by many other objects.

*/

classDotest {//to save a static member variable for a class instance
  Private Static $_instance;//How to construct private tags    Private function__construct () {Echo' This is a constructed method; ';}//To Create a __clone method to prevent an object from being cloned Public function__clone () {Trigger_error(' Clone is not allow! ',E_user_error);
} //A singleton method, a public static method for accessing an instance Public Static functiongetinstance () {if(! (Self::$_instanceinstanceof Self)) { Self::$_instance=NewSelf ;
}returnSelf::$_instance;} Public functionTest () {Echo' Invoke method succeeded ';} } //class with new instantiation of private tag constructor will be error//$datest = new Dotest ();//correct method, double colon:: operator access static method get instance$datest= Dotest::getinstance ();$datest-test ();//Copying (cloning) an object will result in a e_user_error$datest _clone=Clone $datest;

PHP design pattern: Singleton mode

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.