PHP design mode: Singleton mode and PHP design mode

Source: Internet
Author: User

PHP design mode: Singleton mode and PHP design mode

The Singleton mode, as its name implies, has only one instance. As the object creation mode, Singleton mode ensures that a class has only one instance and instantiates the instance and provides it to the entire system.

 

The Singleton mode has three key points:

1. A class can only have one instance.

2. You must create this instance on your own.

3. You must provide this instance to the entire system.

 

Why use the PHP Singleton mode?

  1. one of the major aspects of PHP applications is for databases. There will be a large number of database operations in an application. When using object-oriented development, if you use the singleton mode, this can avoid the resource consumption of a large number of new operations and reduce database connections, so that too many ONS is not easy to occur.

2. If a system requires a class to globally control some configuration information, you can easily implement it using the singleton mode.

3. Easy debugging in a page request, because all code is concentrated in a class, you can set hooks in the class and output logs to avoid var_dump () and echo everywhere.

 

Case:

 

/*** Singleton mode in design mode ** $ _ the instance must be declared as a static private variable * the constructor must be declared as private, to prevent the new class of the external program from losing the singleton mode, * the getInstance () method must be set to public and must be called 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, the object can be used by many other objects. */Class man {// 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 have been instantiated! ';} // Singleton method public static function get_instance () {var_dump (isset (self: $ _ instance); if (! Isset (self ::$ _ instance) {self ::$ _ instance = new self ();} return self ::$ _ instance ;} // prevent the user from copying the object instance private 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 man; // The following Example object $ test = man: get_instance (); $ test-> test (); // copying an object will result in an E_USER_ERROR. // $ test_clone = clone $ test;

 

If any, please correct me.

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.