PHP in a single-case model of the detailed

Source: Internet
Author: User
This article is mainly to share with you a single example of PHP in detail, in fact, the singleton mode is very well understood, the singleton mode as the name implies, there is only one instance, as the object creation mode, the singleton mode to ensure that a class has only one instance, and to instantiate and provide this instance to the whole system.

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.

This example is sufficient to understand the magic of the simple interest pattern in depth:

<?php/** * 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 girlfriend{//Save example Instance private static $_instance in this attribute; The constructor is declared private, preventing direct creation of the object Private function __construct () {echo ' instance initialized!    ';        }//Singleton method public static function Get_instance () {Var_dump (Isset (self::$_instance));        if (!isset (self::$_instance)) {self::$_instance=new self ();    } return self::$_instance;    }//Prevent users from replicating object instances Private function __clone () {trigger_error (' no cloning ', e_user_error);    } function Test () {echo ("Here is a Test"); }}//This is an error because the constructor method is declared as private//$test = new girlfriend;//The Singleton object of the class $test = Girlfriend::get_instance () is given below; $test = Girlfriend::get_instance (); $test->test ();//Copying an object will result in a e_user_error.//$test _clone = Clone $test;

Two calls above: Girlfriend::get_instance () and the constructor is only called once on the initial instance, and the initial var_dump (Isset (self::$_instance)) returns FALSE, None of the subsequent instances have output constructors ... var_dump (Isset (self::$_instance)) all return True ...
There is only one instance of a class that deserves ... I believe this example should be understood in depth.

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.