This article to introduce the PHP design pattern example of the singleton mode method, there is a need to know the friend can enter the reference.
Singleton class:
1. The constructor needs to be marked private, the Singleton class can no longer be instantiated in another class, and can only be instantiated by itself
2. Owning an instance of a save class static member variable
3. Have a public static method that accesses this instance. The [Common getinstance () method instantiates a singleton class and can be detected by the instanceof operator whether the class has been instantiated]
Note: You need to create a __clone () method to prevent objects from being copied
Role:
1.php applications are mainly used in databases, so there is a large number of database operations in one application, and using singleton mode, you can avoid a lot of resources consumed by new operations.
2. If a class is required in the system to control some configuration information globally, it can be easily implemented using singleton mode. Refer to ZF's Frontcontroller section
3. In a page request summary, easy to debug, because all the code is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump,echo everywhere.
Examples of PHP singleton patterns.
code as follows |
copy code |
"!--? php /** * Singleton mode */ Class danli{ //static member variable private static $_instance; //Private constructor Method Private Function __construct () { //Prevent the object from being cloned Public function __clone () { Trigger_error (' clone was not allow! ', e_user_error); } public static function getinstance () { if (! ( Self::$_instance instanceof Self) { Self::$_instance = new Self; } return self::$_instance; } Public Function test () { echo "OK"; } } //error: $danli = new Danli (); $danli _clone = Clone $danli; Correct: $danli = Danli::getinstance (); $danli->test (); ? |
Let's discuss why we use PHP singleton mode.
Most people understand its purpose from the literal meaning of the singleton pattern, and think that it is a kind of "family planning" that saves the system resources and avoids duplication of instantiation. Each time PHP executes the page, it cleans up all the resources from memory. Thus, in PHP, the single instance of the actual operation needs to be re-instantiated, thus losing the meaning of a single case of repeated instantiation. In this respect alone, PHP's singleton is indeed a bit disappointing for you. But does the singleton only have this function and application? The answer is no, let's take a look.
1. PHP is mainly used in database applications, so there will be a large number of database operations in an application, in the use of object-oriented development (nonsense), if you use the singleton mode, you can avoid a large number of new operations to consume resources.
2. If a class is needed in the system to control some configuration information globally, it can be easily implemented using singleton mode. This can be see the Frontcontroller section of the Zend Framework.
3. In a page request, it is easy to debug, because all the code (such as database Operation class DB) is concentrated in a class, we can set the hooks in the class, output the log, so as to avoid var_dump everywhere, echo.
The code is as follows |
Copy Code |
/** * Design pattern of a single case mode * $_instance must be declared as a static private variable * Constructors and destructors must be declared private to prevent external program new * Class to lose the meaning of the singleton pattern * The getinstance () method must be set to public, this method must be called * To return a reference to the instance *:: operator can only access static variables and static functions * New objects will consume memory * Usage Scenario: The most common place is the database connection. * Once an object is generated using singleton mode, * This object can be used by many other objects. */ Class Example { Save example instance in this property private static $_instance; The constructor is declared private, preventing the object from being created directly Private Function __construct () { Echo ' I am construceted '; } Single Case method public static function Singleton () { if (!isset (self::$_instance)) { $c =__class__; Self::$_instance=new $c; } return self::$_instance; } Prevent users from replicating object instances Public Function __clone () { Trigger_error (' Clone is not allow ', e_user_error); } function test () { Echo ("Test"); } } This is an error because the constructor method is declared as private. $test = new Example; The following will get the singleton object of the example class $test = Example::singleton (); $test->test (); Copying an object will result in a e_user_error. $test _clone = Clone $test; ?> |
http://www.bkjia.com/PHPjc/628780.html www.bkjia.com true http://www.bkjia.com/PHPjc/628780.html techarticle This article to introduce the PHP design pattern example of the singleton mode method, there is a need to know the friend can enter the reference. Singleton class: 1. The constructor needs to be marked private, and the Singleton class does not ...