Re: Simple (?) Singleton Question
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Thu, 15 Nov 2007 21:27:09 -0500
Rob Wilkerson wrote:
On Nov 14, 6:33 pm, Michael Fesser <neti...@xxxxxx> wrote:.oO(Rob Wilkerson)
I suppose I just don't understand the concept of "globallyOK. I just meant that you can easily access the singleton from within
available from everywhere in your scripts" if a "new" instance is
actually being created with each request.
every other function or method in your currently running script:
function foo() {
$obj = TMyClass::getInstance();
$obj->doSomething();
}
Wherever you need the singleton, you just have to call the getInstance()
method. It's like the 'global' keyword for importing global variables
into the local scope, but IMHO cleaner and more flexible. Of course the
next HTTP request is an entirely different thing and starts it all over
again.
I guess what I keep coming back to is that this seems to have limited
practical use as a singleton. True, its instantiation becomes global
despite the fact that it may was instantiated (in your example) within
a traditionally scoped element (a function), but that seems like a
minor - and almost accidental - benefit. I don't tend to use
standalone functions that have no context, so scoping issues don't
really come up.
Additionally, my working practice is to use dependency injection
rather than create objects "on demand". That gives me a certain level
of controlled immutability. And, since the length of a request is so
short, it's usually pretty easy to feel reasonably secure that, during
the life of that request, an object instantiated in the "normal"
manner hasn't mutated to any kind of unusable (or unexpected) state
(perhaps I'm just naive?).
Anyway, I'm not trying to be difficult. Just trying to understand how
to best use singletons in a PHP context, their capabilities and their
limitations. You all have been a lot of help and I appreciate it.
I use them all the time. For instance, I might have a database class. I might have several business classes which access tables in the database. Each business class operates independently of the other classes; I may use one or several in any once page.
The singleton class for the database means each of the business classes can get the one instance of the database class. If the database object doesn't exist, it is created and returned. If it does exist, it is just returned.
Saves from having multiple connections to the database unnecessarily yet business classes don't need each create their own classes, and the main page code doesn't worry at all about the database.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- References:
- Simple (?) Singleton Question
- From: Rob Wilkerson
- Re: Simple (?) Singleton Question
- From: ZeldorBlat
- Re: Simple (?) Singleton Question
- From: Rob Wilkerson
- Re: Simple (?) Singleton Question
- From: Michael Fesser
- Re: Simple (?) Singleton Question
- From: Rob Wilkerson
- Re: Simple (?) Singleton Question
- From: Michael Fesser
- Re: Simple (?) Singleton Question
- From: Rob Wilkerson
- Simple (?) Singleton Question
- Prev by Date: Re: Uploads and mime type
- Next by Date: Re: api for shipping with DHL
- Previous by thread: Re: Simple (?) Singleton Question
- Next by thread: Re: Simple (?) Singleton Question
- Index(es):
Relevant Pages
|