Skip to content

Instantly share code, notes, and snippets.

@pchatterjee
Created August 2, 2012 03:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pchatterjee/3232900 to your computer and use it in GitHub Desktop.
Save pchatterjee/3232900 to your computer and use it in GitHub Desktop.
PHP Singleton Pattern Example
<?php
class Singleton
{
protected static $instance = null;
protected function __construct()
{
# Thou shalt not construct that which is unconstructable!
}
protected function __clone()
{
# Me not like clones! Me smash clones!
}
public static function getInstance()
{
if (!isset(static::$instance)) {
echo "Creating Instance \n";
static::$instance = new static;
}
echo "Returning instance \n";
return static::$instance;
}
}
Singleton::getInstance();
Singleton::getInstance();
Singleton::getInstance();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment