Skip to content

Instantly share code, notes, and snippets.

@cakyus
Created October 16, 2012 13:00
Show Gist options
  • Save cakyus/3899126 to your computer and use it in GitHub Desktop.
Save cakyus/3899126 to your computer and use it in GitHub Desktop.
Example of Singleton Pattern
class classB {
private static $instance;
private $count = 0;
private function __construct() {}
static function functionB() {
if (!isset(self::$instance)) {
$className = __CLASS__;
self::$instance = new $className;
}
return self::$instance;
}
public function increment() {
return $this->count++;
}
}
$b = classB::functionB();
echo $b->increment(); // 0
echo $b->increment(); // 1
$b = classB::functionB();
echo $b->increment(); // 2
echo $b->increment(); // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment