) PHP Singleton Mode

Source: Internet
Author: User

First, we need to know the concept of Singleton mode. What is Singleton mode?

The Singleton mode, as its name implies, has only one instance.

As the object creation mode, the singleton mode ensures that a class has only one instance, and the instance is self-instantiated and provided to the entire system,

This class is called a singleton class.

The Singleton mode has three main points:

First, a class can only have one instance;

Second, it must create the instance on its own;

Third, it must provide the instance to the entire system.

Next we will discuss why we should use the PHP Singleton mode?

Most people understand the purpose of the singleton model literally. They think that this is a saving of system resources and can avoid repeated instantiation. It is a kind of "family planning ".PHP clears all resources from the memory after each execution of the page. Therefore, the actual running of a single instance in PHP requires re-instantiation.In this way, the meaning of repeated instantiation of a single instance is lost. In this case, the PHP Singleton is indeed a bit disappointing. But does the singleton only have this function and application? The answer is no. Let's take a look.

1. PHP applications mainly involve database applications. Therefore, a large number of database operations exist in an application. When using the object-oriented method for Development (nonsense), if you use the singleton mode, this avoids the resource consumption of a large number of new operations.

2. If you need a class in the system to globally control some configuration information, you can easily implement it using the singleton mode. For details, refer to the frontcontroller section of Zend framework.

3. In a page request, debugging is easy because allCode(For example, Database Operations dB) are concentrated in a class. We can set hooks in the class and output logs to avoid var_dump and echo everywhere.

Example

 1 <? PHP  2   3   /*  The following is an example of the singleton mode:  4   *  5   * 1. $ _ instance must be declared as a static private variable  6  * 2. constructor and clone functions must be declared private to prevent externalProgramThe meaning of the singleton mode is lost because of the new class.  7   * 3. The getinstance () method must be declared public and must be called to return a reference to a unique instance.  8   * 4.: The operator can only access static variables or static functions.  9   *  10    */  11   12   Class  People {  13       Private  Static   $ _ Instance = Null  ;  14   15       Public   $ Height ='' ;  16   17       Public   $ Age ='' ;  18   19       Private  Function  _ Construct (){  20           $ This -> Height = '20180101' ;  21           $ This -> Age = 25 ;  22   }  23   24       Private   Function  _ Clone (){  25          //  Do something  26   }  27   28       Static   Public   Function  Getinstance (){  29           If (! SELF :: $ _ Instance  Instanceof self ){  30               // Echo 'lgh-big ';  31 SELF :: $ _ Instance = New  Self ();  32 } Else  {  33               //  For testing only  34   // Echo 'gdc-xiaoairener ';  35   } 36   37           Return SELF :: $ _ Instance  ;  38   }  39   40       Public   Function  Getheight (){  41           Echo   $ This -> Height;  42  }  43   44       Public   Function  Getage (){  45           Echo   $ This -> Age;  46   }  47   48   }  49   50  Function  Testinstance (){  51 People: getinstance ()-> Getage ();  52   }  53   54   //  Begin to use the class  55   56   $ Lgh = People :: Getinstance ();  57   $ Lgh -> Getheight ();  58   59   Echo '<Br/>' ;  60   Testinstance ();  61 ?>

Note:
* 5. The single-instance mode of PHP is relatively speaking, because the explain running mechanism of PHP enables all related resources to be recycled after each PHP page is interpreted and executed. That is to say, PHP cannot make an object resident in memory at the language level. In PHP, all variables are page-level. Whether it is a global variable or a static member of the class, they will be cleared after the page is executed, and new objects will be created in the result, this completely loses the meaning of Singleton. However, multiple business logic may exist on the same page in actual application. In this case, the singleton mode plays an important role and effectively avoids repeated new objects (note: the new object will consume memory resources) So we can say that the single-instance mode of PHP is relatively speaking.

PHP Technology Exchange Group 170855791

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.