Skip to content

Instantly share code, notes, and snippets.

@augustohp
Created October 3, 2011 18:23
Show Gist options
  • Save augustohp/1259831 to your computer and use it in GitHub Desktop.
Save augustohp/1259831 to your computer and use it in GitHub Desktop.
PHP Singleton implementation example
<?php
class SingletonPattern
{
private static $instance;
private function __construct()
{
}
public function getInstance()
{
if (!static::$instance instanceof SingletonPattern) {
echo "Create Instance \n";
static::$instance = new self();
}
echo "Returning instance \n";
return static::$instance;
}
}
SingletonPattern::getInstance();
SingletonPattern::getInstance();
SingletonPattern::getInstance();
/*
Output:
Create Instance
Returning instance
Returning instance
Returning instance
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment