Instantly share code, notes, and snippets.

Embed
What would you like to do?
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