Does this have a name? Transparent Singleton

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Does this have a name? Transparent Singleton

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Does this have a name? Transparent Singleton

Post 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.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Does this have a name? Transparent Singleton

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Does this have a name? Transparent Singleton

Post by Christopher »

Join me in a Pan-Pacific beer then? :drunk:

(I'm having one anyway ;)) Cheers :D
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Does this have a name? Transparent Singleton

Post 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:
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Does this have a name? Transparent Singleton

Post by Benjamin »

Nothing wrong with experimenting lol.
Post Reply