|
Posted by Klaus Brune on 10/11/27 11:59
In article <ioWdnS4b1twTYoHYRVnyrA@bt.com>, root@yourbox.com says...
>
>
> 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.
>
>
How about if we take the discussion outside of pure code and into the
real world... I don't know if this will be applicable to a "pure"
singleton pattern as defined in a class. However...
Let's say you have a system where a user logs in, and from that point
you're using sessions to manage the log-in state. Would you allow the
user to say, log in using two or three different web browsers installed
on the same machine? Or (potentially) worse, let them give their log-in
information to friends and co-workers?
Would code that prevents this sort of thing qualify as something that is
attempting a singleton pattern? Say, by checking user_agent, or not
allowing a log-in when the user is already logged in?
Just a bit of wool-gathering, and my 2 cents.
GC
Navigation:
[Reply to this message]
|