SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Hamilton, ON
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP - Objects in PHP

    I have a question about objects in PHP and I can't get clarification anywhere. I am just wondering... when are they terminated after instanciation? Are they stored server side or client side, and are they available (like sessions) until the browser is closed (or they are otherwise terminated)? Or, if you navigate away from the current page are they terminated?

    Thank-you!
    __________________

  2. #2
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instances of objects - and indeed, class definitions themselves - are only available server-side for a single request.

    If you need information to persist for longer than that, you will need to make use of sessions or similar mechanisms.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I like this quote "with PHP, nothing persists between requests, not even bacteria".

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In this regard objects are just variables. They'll die at the end of script execution, or you can destroy them using unset before hand.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Location
    Hamilton, ON
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright thank-you very much everyone. I was just thinking about using objects to store some user information, like username etc. instead of sessions....but I guess that's a no-go!

  6. #6
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depending on what you are trying to accomplish, it might be possible for you to utilize some form of shared memory. These allow you to persist data across all requests; even for different users of your site!

    Some of the more popular means of doing this are Memcache and APC (using apc_store etc.).

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you want to hold a class for users, there's no reason you can't - it's quite easy. Just use serialize() and hold it in a session:
    PHP Code:
    $users = new Users;
    $_SESSION['userSet'] = TRUE;
    $_SESSION['userClass'] = serialize($users); 
    and to retreive the class, use unserialize():
    PHP Code:
    $users unserialize($_SESSION['userClass']); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You only need to use serialize if you are storing objects in a database field or flat file. The session mechanism already serializes values when storing them.

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ah, even better then
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  10. #10
    SitePoint Member
    Join Date
    Oct 2007
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This question I did not quite understand it, to learning!


Bookmarks

Posting Permissions

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