PHP design mode: Single-instance mode

Source: Internet
Author: User

Some days ago began to really understand the design pattern, to start, simply from the singleton mode, of course, read some of the information on the web, a better understanding of the singleton model, look at the introduction, and then look at the code basic also can understand, design patterns these flower point of mind Basic is able to understand, Of course, to be good use of the project is also a need for a certain practice, not just know, or is very strong and understand, one to the actual operation will not be, nonsense will not say more, the comrades on the front of the PHP refueling it;
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)
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.
Code implementation:

1<?PHP2 3 /**4 * Design pattern of a single case mode5 * $_instance must be declared as a static private variable6 * Constructors and destructors must be declared private to prevent external program new, class thereby losing the meaning of singleton patterns7 * The getinstance () method must be set to public, and this method must be called to return a reference to the instance8 *:: operator can only access static variables and static functions9 * New objects will consume memoryTen * Usage Scenario: The most common place is the database connection.  One * Once an object is generated using singleton mode, the object can be used by many other objects.  A  */ - classSingletondemo - { the  -     //to save a static member variable for a class instance -     Private Static $_instance; -  +     //How to construct private tags -     Private function__construct () +     { A         Echo' This is a constructed method; '; 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); in     } -  to     //A singleton method, a public static method for accessing an instance +      Public Static functiongetinstance () -     { the         if(! (Self::$_instanceinstanceof Self)) { *Self::$_instance=NewSelf ; $         }Panax Notoginseng         returnSelf::$_instance; -     } the  +      Public functionTest () A     { the         Echo' Invoke method succeeded '; +     } -  $ } $  - //class with new instantiation of private tag constructor will error - //$singletonDemo = new Singletondemo (); the  - //Correct method, use double colon:: operator to access the static method to get the instanceWuyi $singletonDemo= Singletondemo::getinstance (); the $singletonDemo-test (); -  Wu //Copying (cloning) an object will result in a e_user_error - $singletonDemo _clone=Clone $singletonDemo;

PHP design mode: 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.