PHP Singleton mode and application instance

Source: Internet
Author: User
Tags zend framework
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 it is instantiated and provided to the entire system. This class is called the singleton class. The Singleton mode has three main points:

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 it is instantiated and provided to the entire system. This class is called the singleton class. The Singleton mode has three main points:

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, in PHP, the singleton needs to be re-instantiated every time it is run, thus losing the significance of the singleton repeated instantiation. in this regard, 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 all the code (such as database operation db) is concentrated in one class, we can set hooks in the class and output logs, this avoids var_dump and echo everywhere.

/**
* 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 Example
{
// Save the instance in this attribute
Private static $ _ instance;

// The constructor declares private to prevent direct object creation.
Private function _ construct ()
{
Echo 'I am construceted ';
}

// Singleton Method
Public static function singleton ()
{
If (! Isset (self ::$ _ instance ))
{
$ C =__ CLASS __;
Self: $ _ instance = new $ c;
}
Return self: $ _ instance;
}

// Prevents users from copying object instances
Public function _ clone ()
{
Trigger_error ('clone is not allow', E_USER_ERROR );
}

Function test ()
{
Echo ("test ");

}
}

// This write method will fail because the constructor is declared as private.
$ Test = new Example;

// The following is a singleton object of the Example class.
$ Test = Example: singleton ();
$ Test-> test ();

// Copying an object will result in an E_USER_ERROR.
$ Test_clone = clone $ test;
?>

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.