Page 1 of 1
Singleton and System Resources
Posted: Mon Oct 25, 2010 10:12 pm
by blither
I am wondering how does PHP handle singletons with actual system resources. Is a singleton in PHP broken out into a per session basis or is it globally for every connection on the server? Also, if it happens to be the latter, how does this affect a singleton class that manages a mysqli connection when one can only have one prepared statement at a given time, unless store_result is executed?
Thanks
Re: Singleton and System Resources
Posted: Mon Oct 25, 2010 10:16 pm
by Weirdan
In PHP no object live longer than a request that created that object.
Re: Singleton and System Resources
Posted: Mon Oct 25, 2010 10:22 pm
by blither
What about the singleton being globally accessible? So if two people hit the server within moments of one another, would they each get their own singleton or is the PHP singleton on instance for the server or virtual server?
Thanks for the quick reply.
Re: Singleton and System Resources
Posted: Tue Oct 26, 2010 12:47 am
by josh
Each request spawns a new process. If you need an object on more than one request, you'll have to serialize it's state to some persistent storage. There are a verity of 'ORM frameworks' that map objects to relational data. There is also serialize() and unserialize() functions in PHP which can be used to store the object as if it were a string, the only caveat being the class must be defined before trying to unserialize() it from a string.
I don't see how global state is something you want, but whatever... glad you're considering PHP

Re: Singleton and System Resources
Posted: Tue Oct 26, 2010 7:50 am
by blither
Thanks, no I was more concerned with the behaviour of PHP rather than wishing it actually created a single instance for the server. I have been using PHP for years just never took the time to look into things like that and now I am working on a project that I am doing a bit of a different design than I have ever tried before so now I needed to know

.
Thanks for the answers.