Page 1 of 1

Does this have a name? Transparent Singleton

Posted: Sat Feb 23, 2008 9:06 pm
by Chris Corbyn

Code: Select all

class TransparentSingleton {
 
  private static $_instance = null;
  
  private $_count = 0;  
 
  public function __construct() {
    if (!isset(self::$_instance)) {
      self::$_instance = $this;
    }
  }
 
  public function setCount($count) {
    self::$_instance->_count = $count;
  }
 
  public function getCount() {
    return self::$_instance->_count;
  }
 
}
 
$a = new TransparentSingleton();
$a->setCount(10);
 
$b = new TransparentSingleton();
echo $b->getCount(); //10
 
$b->setCount(5);
 
echo $a->getCount(); //5
I'm not sure it's all that useful; perhaps even terrible. But it does allow singletons to be used with things like PHP's Stream wrappers which take only a class name.

Re: Does this have a name? Transparent Singleton

Posted: Sat Feb 23, 2008 9:30 pm
by Christopher
What is the difference between that and this?

Code: Select all

class Dingleton {
  private static $_count = 0;  
 
  public function setCount($count) {
    self::$_count = $count;
  }
 
  public function getCount() {
    return self::$_count;
  }
}
 
$a = new Dingleton();
$a->setCount(10);
 
$b = new Dingleton();
echo $b->getCount(); //10
 
$b->setCount(5);
echo $a->getCount(); //5
It's really a Static Value Object.

Re: Does this have a name? Transparent Singleton

Posted: Sat Feb 23, 2008 9:37 pm
by Chris Corbyn
True. I think my mind's just running away with me today :lol: Probably not the best day to be writing anything meaningful :P

Re: Does this have a name? Transparent Singleton

Posted: Sat Feb 23, 2008 9:43 pm
by Christopher
Join me in a Pan-Pacific beer then? :drunk:

(I'm having one anyway ;)) Cheers :D

Re: Does this have a name? Transparent Singleton

Posted: Sun Feb 24, 2008 12:13 am
by Chris Corbyn
arborint wrote:Join me in a Pan-Pacific beer then? :drunk:

(I'm having one anyway ;)) Cheers :D
I believe I have one bottle of tiger remaining in the fridge. I shall crack it open :drunk:

Re: Does this have a name? Transparent Singleton

Posted: Sun Feb 24, 2008 12:44 am
by Benjamin
Nothing wrong with experimenting lol.