|
Posted by Michael Fesser on 11/14/07 23:33
..oO(Rob Wilkerson)
>Yeah, I guess I was sort of (illogically) hoping that singletons could
>also manage to defy the fundamental nature of PHP and include a level
>of persistence that would allow me to perform certain operations only
>once.
Nope, not out-of-the-box.
>I suppose I just don't understand the concept of "globally
>available from everywhere in your scripts" if a "new" instance is
>actually being created with each request.
OK. I just meant that you can easily access the singleton from within
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.
>If, somewhere between
>requests, I alter my config then I could have one user using an old
>version (until the next request) and another user using the new one.
>
>I'd like to have more control over when the config is read so that I
>can ensure that it's the only one in play at any given time, but the
>config is loaded in a pseudo front controller, so it's not like I'll
>ever encounter a condition under which it *doesn't* exist. That's
>something, I guess.
OK, understood. But in my own framework I load the current config on
every request. Even the user informations - if someone is logged-in -
are reloaded everytime to instantly reflect any changes (permissions for
example).
>Part of this is academic, too. It doesn't seem like much of a
>singleton if I have to keep loading it over and over and if there's no
>way for me to ensure that every user of my system is using precisely
>the same configuration.
You would have to take care of that yourself if you really need it.
Currently there's nothing like an "application scope" in PHP like it is
in Java with an application server behind it. In PHP every HTTP request
is indepedent from each other, with all the benefits and drawbacks.
Micha
Navigation:
[Reply to this message]
|