Re: Reusable Singleton pattern using PHP5

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

 



On Mon, April 24, 2006 2:18 pm, Simas Toleikis wrote:
>> php6 will include static late binding
>
> Great. Love the other new features, especially namespaces.
>
>> - until then you have 2 choices,
>> create a hack (see below) or stick redundant code into each class
>> you
>> want to be a singleton.
>
> The hack doesn't work (See below). Guess we are left with copy-paste
> thing :-/ Though i wonder if there are any other hacks using some sort
> of classkit/other class manipulation functions?
>
>>     public static function GetInstance()
>>     {
>>         if (!isset(self::$instance)) {
>>                 if (!isset(self::$class)) {
>
> Isn't that self::$class the same "late static binding" thing? :-) As i
> get it, at this point from Singleton::GetInstance() you can't get the
> static $class member of Test class.

I'll be damned if I understand what the big deal is, but there are
other hacks one could employ...

Something along the lines of:

function singleton($class_name){
  static $singletons = array();

  if (isset($singletons[$class_name])){
    return $singletons[$class_name];
  }
  else{
    $singletons[$class_name] = new $class_name;
    return $singletons[$class_name];
  }
}

You'd maybe want to have constructor args and use func_get_args to
pass along the args to a different post-constructor constructor of you
own design.

Yes, this is a hack.

I think it's inferior to the other hack posted.

But it's something one should at least consider and reject.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
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