Re: Reusable Singleton pattern using PHP5
| From: | Richard Lynch | Date: | Tue, 25 Apr 2006 05:43:47 +0000 |
| Subject: | Re: Reusable Singleton pattern using PHP5 | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-234653@lists.php.net to get a copy of this message | ||
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