Created
March 8, 2012 11:27
-
-
Save t10u/2000572 to your computer and use it in GitHub Desktop.
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 | |
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