PHP Single-instance mode

Source: Internet
Author: User

Singleton mode (duty mode): Simply put, an object (before learning the design pattern, need to know more about object-oriented thinking) is only responsible for a specific task; Singleton class: 1. Constructors need to be marked private (access control: Prevent external code from creating objects using the new operator),Singleton classes cannot be instantiated in other classes,can only be instantiated by itself; 2,has a static member variable that holds an instance of the class3.have a public static method that accesses this instance(Commongetinstance ()method to instantiate a singleton class byinstanceofOperator can detect if a class has been instantiated) Additionally, 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. Code implementation:/** * Design mode singleton mode * $_instance must be declared as a static private variable * Constructors and destructors must be declared private, preventing the external program new class from losing the meaning of the singleton pattern * The getinstance () method must be set to public and must be called method to return a reference to an instance of the *:: operator can only access static variables and static functions * new objects consume memory * 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. */
1  classDanli {2  3  //to save a static member variable for a class instance4  Private Static $_instance;5      //How to construct private tags6      Private function__construct () {7           Echo' This is a constructed method; ';8      }9      //To Create a __clone method to prevent an object from being clonedTen       Public function__clone () { One           Trigger_error(' Clone is not allow! ',E_user_error); A      } -   -      //A singleton method, a public static method for accessing an instance the       Public Static functiongetinstance () { -           if(! (Self::$_instanceinstanceof Self)) { -Self::$_instance=NewSelf ; -           } +           returnSelf::$_instance; -      } +   A       Public functionTest () { at       Echo' Invoke method succeeded '; -      } -}
The class that instantiates the private tag constructor with new will error//$danli = new Danli ();//correct method, double colon:: operator access static method Get instance $danli = Danli::getinstance (); $danli- >test ();//Copy (clone) object will result in a e_user_error$danli_clone = Clone $danli;

PHP Single-instance 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.