| 
	
 | 
 Posted by Bit Byte on 07/02/27 11:59 
ZeldorBlat wrote: 
 
> Bit Byte wrote: 
>  
>>Since the webserver loads a PHP script every time a request is issued 
>>from a client, it seems that Singletons are unnessesary in PHP?, since 
>>each time an object is invoked, it is (guaranteed?) to be the only 
>>instance - at least for that thread that is handling the request ? No ? 
>> 
>>If I am missing something quite obvious, please let me know. 
>  
>  
> <?php 
> //Thread starts 
>  
> $a = new Foo(); 
> $b = new Foo(); 
>  
> //Now have two instances of Foo, all in the same thread -- no 
> guarantees here 
>  
> //Thread ends 
> ?> 
>  
 
Hmm, yes, that was a rather obvious refudiation of the point I was  
making. I obviously wasn't making myself clear. What I meant was to say  
was that other than in cases (such as the one above) where the user  
deliberately creates multiple instances of the same class, I can't see  
the relevance of porting this Pattern to PHP - since each request is  
handled in a new thread/(possibly) process. 
 
Case in point. I have wrapped up all of my user management functionality  
in a usermanger class which derives from a Singleton base class  
(following logic I had used in my C++ program). Its only when I started  
using this class that it became obvious to me that the logic was not  
necessarily meaningful in the PHP space - each request to manage a user  
- will (as I understand it) - fork a new process (or maybe just spawn a  
new thread), which will have its own singleton class - blisfully unaware  
of the other's existence - unless ofcourse - the Singleton objects are  
being cached in shmem (shared memory) - otherwise, it all seems a bit  
like a pointless exercise in futility (because in C++ etc, once you  
create a Singleton, it notmally stays memory resident until program  
termination - whereas in PHP, the Singleton is only alive for the  
duration of the request?) - unless again, I am missing something.
 
[Back to original message] 
 |