Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: Maitaining single instance
08-22-2007, 11:27 AM #1
- Join Date
- May 2005
- Thanked 0 Times in 0 Posts
Maitaining single instance
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.Thanks & Regards,
08-22-2007, 01:36 PM #2
- Join Date
- Oct 2006
- Denver, Colorado USA
- Thanked 312 Times in 304 Posts
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.
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.