Re: Singletons

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux