Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AaronFlower/70d6e824fe33babbf1417c589f0300a6 to your computer and use it in GitHub Desktop.
Save AaronFlower/70d6e824fe33babbf1417c589f0300a6 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