Skip to content

Instantly share code, notes, and snippets.

@t10u
Created March 8, 2012 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t10u/2000572 to your computer and use it in GitHub Desktop.
Save t10u/2000572 to your computer and use it in GitHub Desktop.
<?php
trait MySingleton
{
private $_foo = null;
private static $_instance = null;
private function __construct()
{
}
public static function getInstance()
{
if (!isset(self::$_instance)) {
$class = __CLASS__;
self::$_instance = new $class;
}
return self::$_instance;
}
public function setFoo($foo)
{
$this->_foo = $foo;
}
public function getfoo()
{
if ($this->_foo) {
return $this->_foo;
}
return false;
}
}
class Foo
{
use MySingleton;
}
$foo = Foo::getInstance();
$foo->setFoo("foo\n");
echo $foo->getFoo();
$bar = Foo::getInstance();
echo $bar->getFoo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment