Single case mode:
As an object's creation pattern, the singleton pattern ensures that a class has only one instance, and instantiates it and provides it globally to the entire system. Instead of creating an instance copy, it returns a reference to an instance stored within a singleton class.
(1). A static member variable that holds a unique instance of the class is required: private static $_instance;
(2). Constructors and cloning functions must be declared private to prevent the external program new class from losing the meaning of a single case pattern:
Private Function __construct ()
{
$this->_db = Pg_connect (' xxxx ');
}
Private Function __clone ()
{
}
(3). You must provide a public static method (usually the GetInstance method) to access this instance to return a reference to a unique instance:
public static function getinstance ()
{
if (! (Self::$_instance instanceof Self))
{
self::$_instance = new self ();
}
return self::$_instance;
}
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 the use of a single example mode can be easily implemented.