To achieve this, you must ensure that the entire project has and only one instance exists and provides an access point for global access. This requires access to this instance by invoking a common static method of this class, which means that the class cannot be instantiated outside the class, only within the class inner instance, and only through the common static method invocation instance;
Recently in the reading of PHP advanced programming, mode, framework and testing a book, quite sentiment, share with you:
When doing a project, we usually expect some class instances to be sufficient once, such as shared configuration classes, template operations classes, database connections, and so on, which are common in the overall project development and consume resources if the instances are multiple instances.
To achieve this, you must ensure that the entire project has and only one instance exists and provides an access point for global access. This requires access to this instance by invoking a common static method of this class, which means that the class cannot be instantiated outside the class, only within the class inner instance, and only through the common static method invocation instance;
So how do you guarantee that this class cannot be instantiated outside? This singleton class must have a constructor __construct and be set to private (because there is already __construct, regardless of the default constructor method: The case of a method that uses the class name directly), so that it is not instantiated directly outside! It is also necessary to declare a static method variable to hold an instance of this class, a common static method to access this instance (because it is to be shared, so it is declared static and holds the shared code area portion in memory). You also need to create an empty private __clone () method to prevent it from being cloned.
A typical singleton class is as follows:
| The code is as follows |
Copy Code |
Class Simple { /** * Examples of singleton classes * * @static * @var Object Simple */ public static $_instance;
Other member variables
/** * Construction Method * * @return void */ Private Function __construct () { Write construction method }
/** * Singleton Mode Invocation method * * @static * @return Object Template */ public static function getinstance () { if (!self::$_instance instanceof self) Self::$_instance = new self (); return self::$_instance; }
/** * Declare an empty private clone method * */ Private Function __clone () { }
Other methods }
Invocation mode $Simple = Simple::getinstance (); ?> |
The above uses the INSTANCEOF keyword, which is the comparison operator, which returns Boolean to determine whether an object instance is a defined type, or whether it inherits from a class, or whether a particular interface is implemented
http://www.bkjia.com/PHPjc/628959.html www.bkjia.com true http://www.bkjia.com/PHPjc/628959.html techarticle to achieve this, you must ensure that the entire project has and only one instance exists and provides an access point for global access. This is required by invoking a common static method of this class to ...