Php Singleton Mode

Source: Internet
Author: User
Singleton mode: Singleton class: 1. constructor needs to be marked as private (Access Control: prevents external code from using the new operator to create an object). The Singleton class cannot be instantiated in other classes, it can only be instantiated by itself; 2. It has a static member variable for an instance that saves classes; 3. It has a public static method to access this instance (commonly used getInstanc

Singleton mode: Singleton class: 1. constructor needs to be marked as private (Access Control: prevents external code from using the new operator to create an object). The Singleton class cannot be instantiated in other classes, it can only be instantiated by itself; 2. It has a static member variable for an instance that saves classes; 3. It has a public static method to access this instance (commonly used getInstanc

Singleton mode:


Singleton class:
1. the constructor must be marked as private (Access Control: prevents external code from using the new operator to create objects). A singleton class cannot be instantiated in other classes but can only be instantiated by itself;
2. static member variables of an instance that stores classes
3. There is a public static method to access this instance (the getInstance () method is often used to instantiate the singleton class, And the instanceof operator can detect whether the class has been instantiated)
In addition, you must create the _ clone () method to prevent objects from being copied (cloned)
Why use the PHP Singleton mode?
1. php applications mainly lie in database applications. Therefore, a large number of database operations exist in an application. Using the singleton mode can avoid the resources consumed by 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 ZF.
3. In a page request, debugging is easy. Because all the code (such as database operation db) is concentrated in a class, we can set hooks in the class and output logs, this avoids var_dump and echo everywhere.
Code implementation:
/1 **
* Singleton mode in Design Mode
* $ _ Instance must be declared as a static private variable
* Constructor and destructor must be declared as private to prevent external programs from new
* Class, thus losing the significance of Singleton Mode
* The getInstance () method must be set to public and must be called
* To return a reference to the instance
*: The operator can only access static variables and static functions.
* New objects consume memory.
* Use Cases: database connections are the most common scenarios.
* After an object is generated in singleton mode,
* This object can be used by many other objects.
*/
Class Singleton {

// Save the static member variables of the class instance
Private static $ _ instance;

// Construct a private tag
Private function _ construct (){
Echo 'this is a Constructed method ;';
}

// Create the _ clone method to prevent objects from being copied and cloned.
Public function _ clone (){
Trigger_error ('clone is not allow! ', E_USER_ERROR );
}

// Singleton method, which is a public static method used to access the instance
Public static function getInstance (){
If (! (Self: $ _ instance instanceof self )){
Self: $ _ instance = new self;
}
Return self: $ _ instance;
}

Public function test (){
Echo 'method invoked successfully ';
}

}

// An error will be reported when the class that marks the constructor with the new instantiated private flag
// $ Singleton = new Singleton ();

// Correct method. Use the double Colon: operator to access the static method to obtain the instance
$ Singleton = Singleton: getInstance ();
$ Singleton-> test ();

// Copying (cloning) the object will cause an E_USER_ERROR
$ Singleton _ clone = clone $ Singleton;


/*

Trigger_error () function creates a user-defined error message

Trigger_error (error_message, error_types)
Error_type: 1 E_USER_ERROR

2E_USER_WARNING

3 E_USER_NOTICE

*/


?>

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.