Php Singleton mode

Source: Internet
Author: User
One of the main application scenarios of PHP is the application scenario where applications interact with databases. Therefore, a large number of database operations exist in an application. for example, if a database handle is used to connect to a database, the Singleton mode can avoid a large number of new operations, because each new operation will consume... "/> <scripttype =" text/javascript "src =" http: // www.2

One of the main application scenarios of PHP is the application scenario where applications interact with databases. Therefore, a large number of database operations exist in an application. for example, if a database handle is used to connect to a database, the Singleton mode can avoid a large number of new operations, because each new operation consumes memory resources and system resources.

 

[Advantages of Singleton mode]
1. controlled access to a unique instance
2. narrowing down the namespace Singleton mode is an improvement for global variables. It avoids the global variables that store unique instances from polluting the namespace.
3. a subclass can be added to the list class of operations and representations. In addition, it is easy to configure an application using this extension class instance. You can use the instance of the class you need to configure the application at runtime.
4. allow variable target instances (multi-sample mode)
5. more flexible than class operations

[Applicable scenarios of Singleton mode]
1. when the class can only have one instance and the customer can access it from a well-known access point
2. this unique instance should be extensible through subclass. And the user should be able to use an extended instance without changing the code.

[Singleton mode and other modes]
Factory method mode: The Singleton mode uses factory mode to provide its own instances.
Abstract factory mode: the abstract factory mode can use the Singleton mode to design a factory class into a singleton class.
Builder mode: you can design a specific build class into a singleton mode.

 

3. Singleton mode instance

3.1 test. php


 

SPAN style = "FONT-SIZE: 14px">
 Service = new mysqli (self ::$ SDB_HOST, self ::$ SDB_USER, self ::$ SDB_PASS, self ::$ SDB_DBNAME) ;}// private clone function, prevent External clone object private function _ clone () {}// static method. for a single instance, the unified access entry public static function getInstance () {if (! Isset (self: $ instance) | is_null (self: $ instance) {self: $ instance = new Test ();} return self: $ instance ;} // test method: print hello, world public function sayHello () {echo 'Hello, world ';}} 
 Service = new mysqli (self ::$ SDB_HOST, self ::$ SDB_USER, self ::$ SDB_PASS, self ::$ SDB_DBNAME) ;}// private clone function, prevent External clone object private function _ clone () {}// static method. for a single instance, the unified access entry public static function getInstance () {if (! Isset (self: $ instance) | is_null (self: $ instance) {self: $ instance = new Test ();} return self: $ instance ;} // test method: print hello, world public function sayHello () {echo 'Hello, world ';}}

3.2 data. php


 

SPAN style="FONT-SIZE: 14px">
 test = Test::getInstance();         $this->test->sayHello();     }    }  $obj = new TestObject(); 
 test = Test::getInstance();  $this->test->sayHello(); } }

$ Obj = new TestObject ();
Run data. php. result:

 

 
 

 

4. the core points of PHP Singleton mode are as follows:
1. a static member variable (usually $ instance private variable) that stores the unique instance of the class is required)
2. constructor and clone functions must be declared private. this is to prevent external programs from losing the meaning of Singleton mode.
3. a public static method (usually the getInstance method) must be provided to access this instance to return a reference of a unique instance.
5. disadvantages of the PHP Singleton mode
As we all know, PHP is an interpreted scripting language. this running mechanism allows every PHP page to be interpreted and executed, and all related resources will be recycled. That is to say, PHP cannot make an object resident in the memory at the language level, which is different from asp.net, Java, and other compilation types, for example, in Java, the single meeting always exists throughout the application lifecycle, and variables are cross-page-level, so that this instance can truly be unique in the application lifecycle. However, in PHP, all variables, whether global variables or static members of the class, are page-level. every time a page is executed, a new object will be created, will be cleared after the page is executed, so it seems that the PHP Singleton mode is meaningless, therefore, the PHP Singleton mode makes sense only when multiple application scenarios occur during a single page-level request and the same object resource needs to be shared.

 


 

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.