Skip to content

Instantly share code, notes, and snippets.

@augustohp
Created October 3, 2011 18:23
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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