PHP Singleton mode---(disappointment)

Source: Internet
Author: User
Tags zend framework

The singleton pattern, as its name implies, is only one instance.

As the creation mode of an object, the singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system.

This class is what we call a singleton class.

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.

Let's discuss why we use PHP singleton mode.

Most people understand its purpose from the literal meaning of the singleton pattern, and think that it is a kind of "family planning" that saves the system resources and avoids duplication of instantiation. each time PHP executes the page, it cleans up all the resources from memory . Thus, in PHP, the single instance of the actual operation needs to be re-instantiated , thus losing the meaning of a single example of repeated instantiation . In this regard alone, PHP's singleton is indeed a bit disappointing for you. But does the singleton only have this function and application? The answer is no, let's take a look.

1. PHP is mainly used in database applications, so there will be a large number of database operations in an application, in the use of object-oriented development (nonsense), if you use the singleton mode, you can avoid a large number of new operations to consume resources.

2. If a class is needed in the system to control some configuration information globally, it can be easily implemented using singleton mode. This can be see the Frontcontroller section of the Zend Framework.

3. In a page request, it is 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. $_instance must be declared as a static private variable
* 2. Constructors and clone functions must be declared private, in order to prevent the external program new class from losing the meaning of the singleton pattern
* 3. The getinstance () method must be declared as public, and this method must be called to return a reference to a unique instance
* 4. :: operator can only access static variables or static functions
* 5. PHP's singleton mode is relatively relative, because PHP's interpretation of the operating mechanism so that each PHP page is interpreted after execution, all the relevant resources will be recycled .
* That is,PHP has no way of keeping an object resident in memory at the language level . In PHP, all variables are page-level , regardless of global variables,
* or the static members of the class, will be emptied after the page executes, and the result will re-establish the new object , which will completely lose the meaning of Singleton.
* However, in the actual application of the same page may have multiple business logic, then the singleton mode plays a very important role, effectively avoid duplication
* New Object (note: The new object consumes memory resources) such a behavior, so we say that the PHP singleton mode is relatively

A simple example

classuser{//to save a static member variable for a class instance    Private Static $_instance;  Public  $_name; //How to construct private tags    Private function__construct () {Echo' This is a constructed method;<br> '; }         //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; } }

PHP Singleton mode---(disappointment)

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.