View Full Version : Maitaining single instance

08-22-2007, 12:27 PM
Hi All,

I need to maintain an object instance in to memory, for the whole php application. Is this possible in php.

My scenario, create an single instance when the php application is first loaded, this instance should be available to make use of it, to all logged in users. Unlike sessions, this should not be created whenever an user logs in to the appln.

Is there any provision in php to maintain something through out the application, not during only the session.

08-22-2007, 02:36 PM
Because web servers are stateless, they don't know and don't care what requests happened before or what will happen after the current request. The only reasonable way to share information between more than one visitor is to store it in a database.

Once each request has been served by the web server, all resources allocated to that one request are destroyed. The only way to share data for any one visitor between visits is to propagate it in a session/cookie or a database (in which case you must propagate an id relating that data to the visitor in a cookie/url parameter...)

Sessions are per visitor, so no script would normally know all the active session ID's. You could store all the current session ID's in a database, then go through and open and copy all the different session data on each page visit (assuming that the file system would permit multiple access to the session data files), but all of this file access would be much slower than just storing all the data in a database in the first place and retrieving it in one query.

I am guessing that you are asking this about a multi-player game? Use a database to store the current state of the game. It is the only reasonable and easy way to do this.