Why use PHP as a single case mode?
1, the application of PHP is mainly in the database application, so an application will have a large number of database operations, the use of single case mode, you can avoid a large number of new operations to consume resources
2, if the system needs to have a class to global control of some configuration information, then use a single example mode can be very convenient to implement. This can be frontcontroller part of ZF.
3, in a page request, easy to debug, because all the code (such as database Operation class DB) are concentrated in a class, we can set hooks in the class, output log, so as to avoid everywhere var_dump, Echo
Single Case class
1. Constructors need to be marked private (access control: Prevents external code from using the new operator to create an object), a singleton class cannot be instantiated in another class, and can only be instantiated by itself
2. Have a static member variable that holds an instance of the class
3. Have a public static method to access this instance (commonly used getinstance () method to instantiate a singleton class, through the instanceof operator can detect whether the class has been instantiated)
4. In addition, you need to create a __clone () method to prevent objects from being replicated (clones)
A single example implementation method
Single example class demo source code
/**
* $_instance must be declared as a static private variable
* Constructors and destructors must be declared private to prevent the external program new
* class from losing the meaning of a single case pattern
* getinstance () method must be set to public , you must call this method
* to return a reference to the instance
*:: operator can only access static variables and static functions
* New objects consume memory
* Usage scenarios: The most common place is the database connection.
* After an object is generated using a single case pattern,
the object can be used by many other objects.
*/class Instancedemo {
//Save static member variable of class instance
private static $_instance;
The private tag's construction method
Private Function __construct () {
echo′this is a constructed method;′;
}
Create the __clone method to prevent the object from being replicated clone public
function __clone () {
trigger_error (′clone is not allow!′,e_user_error);
//A singleton method, which is used to access the public static method of the instance (
getinstance) {
if (! Self::$_instance instanceof Self)) {
self::$_instance = new self;
}
return self::$_instance;
}
Public
function Test () {
echo′ invoke method succeeded;
}
}
Call method
Correct method, with double colon:: operator access static method to get instance $demo = Instancedemo:: getinstance (); $demo->test ();
To put it simply, an object (before learning design patterns, need to know more about object-oriented thinking) only responsible for a specific task, read some information on the Internet, a single example of a better understanding, look at the introduction, and then look at the basic code can be understood, design patterns of these ideas are basic to understand, Whether Java or C #, or PHP design patterns are basically universal, the above code, this completes a simple single example class, hope for everyone useful.