Created
June 28, 2014 20:50
-
-
Save chrisguitarguy/7cb6bebf2172b6156277 to your computer and use it in GitHub Desktop.
An example of a "default singleton"
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 Foo | |
{ | |
private $name; | |
private static $defaultInstance = null; | |
/** | |
* Public constructor: create as many instance as you want | |
*/ | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
} | |
public static function getDefaultInstance() | |
{ | |
if (null === self::$defaultInstance) { | |
self::$defaultInstance = new self('default'); | |
} | |
return self::$defaultInstance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment