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
Singleton and System Resources
Moderator: General Moderators
Re: Singleton and System Resources
In PHP no object live longer than a request that created that object.
Re: Singleton and System Resources
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.
Thanks for the quick reply.
Re: Singleton and System Resources
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
I don't see how global state is something you want, but whatever... glad you're considering PHP
Re: Singleton and System Resources
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.
Thanks for the answers.