Re: Singletons
| From: | Carlos Medina | Date: | Wed, 22 Oct 2008 09:33:24 +0000 |
| Subject: | Re: Singletons | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-282238@lists.php.net to get a copy of this message | ||
Hi,
here is in my opinion, what you can do:
class Base
{
private $foo;
private function __construct()
{}
public function getFoo()
{
return $this->foo;} public function setFoo( $foo ) {
$this->foo = $foo;} } class Singleton extends Base { public function __construct() {} private function __clone() {} public static function getInstance() {
static $instance = null;
if (!isset($instance))
$instance = new self();
$instance->setFoo( 'singleton' );
return $instance;
}
}
$bar = Singleton::getInstance();
echo $bar->getFoo(); // "Singleton"
Regards
Carlos