PHP Object-oriented single-instance mode, PHP object-oriented value mode
Singleton mode (duty mode): Simply put, an object (before learning the design pattern, need to know more about object-oriented thinking) is only responsible for a specific task; Singleton class: 1. Constructors need to be marked private (access control: Prevent external code from creating objects using the new operator), A singleton class cannot be instantiated in another class, only instantiated by itself; 2. A static member variable 3 that holds an instance of the class, and a public static method that accesses the instance (the Common getinstance () method instantiates a singleton class, You can detect if a class has been instantiated by using the instanceof operator in addition, you need to create a __clone () method to prevent an object from being copied (cloned) Why use PHP singleton mode? 1, the application of PHP is mainly in the database application, so there will be a large number of database operations in an application, using a singleton mode, you can avoid a large number of new operations to consume resources. 2, if the system needs to have a class to control some of the configuration information globally, then using the singleton mode can be easily implemented. This can be see the frontcontroller part of ZF. 3, in a page request, 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. Code implementation: A singleton pattern of <111?111php111/1*** Design patterns * $_instance must be declared as static private variables * Constructors and destructors must be declared private, preventing external programs from new* classes to lose the meaning of singleton patterns * The 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. * Once an object is generated using singleton mode, the object can be used by many other objects. */class Danli {//Save static member variable of class instance private static $_instance;//private tag construction method private Function __construct () {echo ' This is a Co Nstructed method; ';} The __clone method is created to prevent the object from being copied to the Clone public Function __clone () {Trigger_error (' clone was not allow! ', e_user_error);}//Singleton method, public static method for accessing an instancestatic function getinstance () {if (!) ( Self::$_instance instanceof self) {self::$_instance = new Self;} return self::$_instance;} Public Function test () {echo ' call method succeeded ';}} The class that instantiates the private tag constructor with new will have an error//$danli = new Danli (); Correct method, use double colon:: operator to access static method to get instance $danli = Danli::getinstance (); $danli->test (); Copying (cloning) an object will result in a e_user_error$danli_clone = Clone $danli;
http://www.bkjia.com/PHPjc/1125245.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125245.html techarticle PHP Object-oriented value singleton mode, PHP object-oriented value pattern Singleton mode (duty mode): Simply put, an object (before you learn design patterns, you need to understand the object-oriented thinking ...)