Simple implementation of the php Singleton mode _ php instance

Source: Internet
Author: User
The following is a simple implementation of the php Singleton mode. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian. Simple implementation of php Singleton mode

<? Php/*** Singleton mode of design mode ** $ _ instance must be declared as a static private variable * the constructor and Destructor must be declared as private, this prevents the new * class of the external program from losing the Singleton mode. * The getInstance () method must be set to public. you must call this method * to return a reference to the instance *:: operators can only access static variables and static functions * new objects consume memory * use cases: database connections are the most common. * After an object is generated in Singleton mode, * this object can be used by many other objects. */Class Example {// Save the instance as a private static $ _ instance in this attribute; // The constructor declaration is private to prevent the object private function _ construct () from being directly created () {echo 'I am construceted';} // singleton () {if (! Isset (self ::$ _ instance) {$ c =__ CLASS __; self ::$ _ instance = new $ c;} return self ::$ _ instance ;} // prevent the user from copying the object instance 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 Example object $ test = Example: singleton (); $ test-> test (); // copying an object will result in an E_USER_ERROR. $ test_clone = clone $ test;?>

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.

The simple implementation method of the above php Singleton mode is to share all the content with you, hoping to give you a reference, and hope you can support your feet.

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.