Created
October 3, 2011 18:23
-
-
Save augustohp/1259831 to your computer and use it in GitHub Desktop.
PHP Singleton implementation example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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