Singleton and System Resources

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
blither
Forum Newbie
Posts: 7
Joined: Mon Oct 25, 2010 9:47 pm

Singleton and System Resources

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Singleton and System Resources

Post by Weirdan »

In PHP no object live longer than a request that created that object.
blither
Forum Newbie
Posts: 7
Joined: Mon Oct 25, 2010 9:47 pm

Re: Singleton and System Resources

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Singleton and System Resources

Post 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 :-D
blither
Forum Newbie
Posts: 7
Joined: Mon Oct 25, 2010 9:47 pm

Re: Singleton and System Resources

Post 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.
Post Reply