Re: Singletons

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

 



>>   public function __construct()
> A singleton would usually have a private constructor to prevent
> non-singleton instances.

The problem being if the class in question derives from another class
that has a public constructor...  If you are in that particular
situation (which I am), you're basically SOL and the statement above
has no bearing.

>>   {
>> echo 'singleTon::__construct()<br>';
>>     if( !is_null( singleTon::$thisObj ))
>>     {
>> echo '$thisObj already set.  returning it...<br>';
>>       return singleTon::$thisObj;
>>     }
>>     singleTon::$thisObj = $this;
> 1) You don't return it unless it already exists, not that it matters because
> this is the constructor and you can't return anything from that.

> 2) The constructor has no involvement in management of the singleton
> instance so this is just all wrong.

I did this mainly to see if it would have any effect.

>>   public static function singleton()
>>   {
> That method has the same name as the class. I'm not sure what effect this
> will have but it's certainly to be discouraged.

It has no effect.  And yes, that is true in general, but this is just
a test foobar class.

> Here's the simplest example I can think of (untested, typed straight into my
> mail client)...

This won't work if derived from a class with a public constructor.
That's why my test class' constructor was defined public.

> Singletons are not rocket science, but as with all patterns you really need
> to understand the theory before trying to implement and use it.

Agreed.  But apparently implementing them in PHP leaves things to be desired.

thnx,
Christoph

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