Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created June 28, 2014 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisguitarguy/7cb6bebf2172b6156277 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/7cb6bebf2172b6156277 to your computer and use it in GitHub Desktop.
An example of a "default singleton"
<?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