PHP Database Singleton Schema Understanding

Source: Internet
Author: User
Tags php database

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. The constructor 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 whether the class has been instantiated)

The main points of the singleton pattern are three:

One is that a class can have only one instance;
The second is that it must create this instance on its own;
Thirdly, it must provide this instance to the whole system on its own.

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 new operations to consume resources.
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 the frontcontroller part of ZF.
3, in a page request, easy to debug, because all the code (such as database Operation class DB) is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump everywhere, echo.

1     2<?PHP3/1**4*A singleton pattern of design Patterns5*$_instance must be declared as a static private variable6* Constructors and destructors must be declared as private,prevent external program new7*class to lose the meaning of a singleton pattern8* The getinstance () method must be set to public,This method must be called9*to return a reference to the instanceTen* ::operators can only access static and static functions One*new objects will consume memory A* Usage Scenarios:The most common place is the database connection.  -*once an object is generated using singleton mode, -*The object can be used by many other objects.  the*/ - classDanli { - //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; '; A } at //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 () { in if(! (Self::$_instanceinstanceof Self)) { -Self::$_instance=NewSelf ; to } + returnSelf::$_instance; - } the  Public functionTest () { * Echo' Invoke method succeeded '; $ }Panax Notoginseng } - //class with new instantiation of private tag constructor will error the //$danli = new Danli (); + //Correct method, use double colon:: operator to access the static method to get the instance A $danli= Danli::getinstance (); the $danli-test (); + //Copying (cloning) an object will result in a e_user_error - $danli _clone=Clone $danli;

PHP Database Singleton Schema Understanding

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.