This article mainly introduces PHP simple implementation of the single-state design mode, simple analysis of the configuration of the single-state design mode, implementation and use of methods, the need for friends can refer to the next
In this paper, we describe the simple method of implementing single-state design mode in PHP. Share to everyone for your reference, as follows:
The single-state design pattern typically consists of the following three points:
· A private construction method; (Ensure that the user cannot instantiate it by creating an object)
· A public static method; (responsible for instantiating itself)
· a private static property; (for saving only one instantiated object)
<?phpclass singleton{ //For saving only one instantiated object private static $Instance =null; The constructor method uses the private wrapper to create the object Private function __construct () {} only within the class using new. This method is the only way to return an object in this class, which is a static method that calls public static getinstance () { if (self:: $Instance instanceof-Self) with the class name {// If $instance in this class is empty, the description has not been instantiated too self :: $Instance =new SingleTon ();//Instantiate this object } return self :: $Instance; }} $instance =singleton::getinstance (); You can only use the static method getinstance () to get the object of the Singleton class?>
The so-called single-state design pattern is that a class can only produce/create a unique object
to write a singleton design pattern, you must have a class instantiate only one object, and to make a class only instantiate an object, first let a class not instantiate an object