PHP design mode single-instance mode

Source: Internet
Author: User

The singleton pattern, as the name implies, is only one instance, as the object's creation mode, the singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance to the system as a whole.

Three points of the singleton pattern:

1. A class can have only one instance.

2. You must create this instance yourself.

3. This instance must be provided to the entire system on its own.

Why use PHP singleton mode?

  1.PHP application has a large aspect of the database, there will be a large number of database operations in an application, in the use of object-oriented development, if the use of Singleton mode, you can avoid a large number of new operations consumed by the resources, but also reduce the database connection, so it is not easy to appear too many Connections situation.

2. If a class is needed in a system to control some configuration information globally, it can be easily implemented using singleton mode.

3. It is easy to debug in one page request because all the code is in one class, you can set the hooks in the class, output the log, avoid var_dump (), echo everywhere.

Case:

/** * Design mode singleton mode * $_instance must be declared as a static private variable * The constructor 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, This method must be called to return a reference to an instance of *:: Operators 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.    */class man{//Save example Instance private static $_instance in this attribute; The constructor is declared private, preventing direct creation of the object Private function __construct () {echo ' I was instantiated!    ‘;                }//Singleton method public static function Get_instance () {Var_dump (Isset (self::$_instance));        if (!isset (self::$_instance)) {self::$_instance=new self ();    } return self::$_instance;    }//prevents the user from copying the object instance private function __clone () {Trigger_error (' clone is not allow ', e_user_error);    } function Test () {echo ("test"); }}//This notation is error, because the constructor method is declared as private//$test = new man;//The following will get the example class of the Singleton object $test = Man::get_instance (); $test = man::get_ Instance (); $test->test ();//Copying an object will result in a e_user_error.//$test _clone = Clone $test;

If anything is wrong, please correct me.

PHP design mode single-instance mode

Related Article

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.