Go PHP Single-instance mode

Source: Internet
Author: User
First, we need to know the concept of a singleton pattern, so what is a singleton pattern?

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 case of repeated instantiation. In this respect 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 hooks in the class, output the log, so as to avoid var_dump everywhere, echo

Example

1
 Height = ' 185 '; $this, age = 25;22}23 private Function __clone () {//do something26 }27 static public Function getinstance () {if (!self::$_instance instanceof self) {//EC             Ho ' lgh-big '; self::$_instance = new self (); or else {//for testing only34 Echo ' Gdc-xiaoairener ';}36 Notoginseng return self::$_instance;38}39-Public Function getheight ( ) {echo $this-height;42}43-Public Function getage () {echo $this-age;46} }49 function Testinstance () {people::getinstance () getage ();}53 to use the//begin 56 $lgh = People::getinstance (); $lgh-getheight ();
'; Testinstance ();?>

Other Special note:
* 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. In other words, PHP has no way of keeping an object resident in memory at the language level. In PHP, all variables are page-level, whether global variables, or static members of the class, will be emptied after the completion of the page execution, the result will re-establish new objects, so it completely lost 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 repeating the new object (note: The new object consumes memory resources) such a behavior, so we say that the PHP singleton mode is relatively

PHP Technology 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.