browser lang:en
The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a method for limiting the number of instances of an object to just one. It's an easy pattern to grasp once you get past the strange syntax used.
Consider the following class:
class Database {
public function __construct() { ... }
public function connect() { ... }
public function query() { ... }
...
}
$pDatabase = new Database();
$aResult = $pDatabase->query('...');
class Database {
// Store the single instance of Database
private static $m_pInstance;
...
}
class Database{
// Store the single instance of Database
private static $m_pInstance;
private function __construct() { ... }
}
$pDatabase = new Database();
public static function getInstance(){
if (!self::$m_pInstance){
self::$m_pInstance = new Database();
}
return self::$m_pInstance;
}
class Database{
// Store the single instance of Database
private static $m_pInstance;
private function __construct() { ... }
public static function getInstance(){
if (!self::$m_pInstance){
self::$m_pInstance = new Database();
}
return self::$m_pInstance;
}
}
$pDatabase = new Database();
$aResult = $pDatabase->query('...');
$pDatabase = Database::getInstance();
$aResult = $pDatabase->query('...');
Have you ever had to develop something yourself only to find out that there had already been…
in:JQuery (0 comments)The situation begins with your blog or website and you need to post some code on a particular…
in:The Holy Faq's (0 comments)So. Google just recently announced Google Buzz. I’m not sure about you, but I…
in:Blog (0 comments)There's lots of clever scripts around to tell you how to get images in and out of…
in:PHP scripts (0 comments)Spherical Law of Cosines Suppose that we want to find the five nearest places to (47.470779, -87.890699) using Spherical…
in:MySql (0 comments)I have always used Dreamweaver, and love it. But, I have been thinking, what are the FREE CSS…
in:Free Software (0 comments)Advanced Linux Sound Architecture (known by the acronym ALSA) is a Linux kernel component intended to replace the…
in:Blog (0 comments)The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…
in:Scripts and tutorials (0 comments)
