Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    May 2005
    Thanked 0 Times in 0 Posts

    Maitaining single instance

    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.
    Thanks & Regards,
    Srikanth S

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Denver, Colorado USA
    Thanked 340 Times in 332 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.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts