PHP Singleton mode _php Tutorial

Source: Internet
Author: User
One of the main applications of PHP is application scenarios for dealing with databases, so there are a number of database operations in an application, such as the behavior of database handlers to connect to a database, and the use of Singleton mode to avoid a large number of new operations. Because each new operation consumes memory resources and system resources.

"Advantages of the Singleton mode"
1. Controlled access to the unique instance
2. Narrowing the namespace singleton mode is an improvement on global variables. It avoids those global variables that store unique instances. Pollution namespaces
3, allow the operation and expression of the essence of the Singleton class can have subclasses. It is also easy to configure an application with an instance of this extension class. You can configure your app at run time with an instance of the class you need.
4. Allow variable number of instances (multi-case mode)
5. More flexible than class operation

"Singleton Mode for Scenarios"
1. When a class can have only one instance and the customer can access it from a well-known access point
2. When this unique instance should be extensible by subclasses. And the user should be able to use an extended instance without changing the code.

"Singleton mode and other modes"
Factory method Mode (Factory mode): Singleton mode uses Factory mode to provide its own instance.
Abstract Factory mode: Abstract Factory mode you can use singleton mode to design specific factory classes as singleton classes.
Builder Mode: Construction mode can be used to design a specific construction class into a singleton pattern

3. Example of a singleton pattern

3.1 test.php


SPAN style= "font-size:14px" >
 Service = new mysqli (self:: $SDB-_host, Self:: $SDB-_user, Self:: $SDB-_pass, self:: $SDB _dbname); }//Privatization clone function to prevent the external cloning of objects Private Function __clone () {}//static method, single-instance unified Access Portal public static Functio n getinstance () {if (!isset (self:: $instance) | | \ is_null (self:: $instance)) {self:: $instance = new Tes         T ();     } 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); }//privatized clone function to prevent external cloning of objects Private Function __clone () {}//static method, single access portal 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, results:


The core points of the 4.PHP single-instance mode implementation are as follows three:
1. A static member variable (usually $instance private variable) that holds a unique instance of the class is required
2. Constructors and clone functions must be declared private, in order to prevent the external program new class from losing the meaning of the singleton pattern
3. A common static method that accesses this instance (usually the GetInstance method) must be provided to return a reference to a unique instance
Disadvantages of 5.PHP Singleton mode
As is known to all, the PHP language is an interpreted scripting language, which allows each PHP page to be interpreted and all related resources will be recycled. In other words, PHP does not have a language level to allow an object to reside in memory, which is different from the compilation of ASP. NET, Java, such as the single-instance in Java has been in the entire application life cycle, variables are cross-page level, It is true that this instance is unique in the application life cycle. However, in PHP, all variables, whether global variables or static members of the class, are page-level, each time the page is executed, the new object will be re-created, will be emptied after the page executes, so it seems that PHP singleton mode is meaningless, So PHP singleton mode I think it makes sense to just have multiple scenarios for a single page-level request and need to share the same object resource.


http://www.bkjia.com/PHPjc/477183.html www.bkjia.com true http://www.bkjia.com/PHPjc/477183.html techarticle one of the main applications of PHP is the application scenario for dealing with databases, so there are a number of database operations in an application, such as a database handle to connect data ...

  • 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.