SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ?OOP Basics- Please explain

    I seem to have yet another whole in my knowledge of OOP theory.
    While I have managed a workaround, I can't seem to misunderstand the nature of a object.
    Why on page reload, does an object lose it's properties.
    The object isset, is an instance of my Class, and performs methods of the class...
    except it's properties are NOT set.

    I don't understand why.
    Can someone please explain.
    Thanks!

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This has nothign to do with OOP,

    This is due to the stateless nature of the web, a very different dynamic than desktop applications (or java applets)

    When an interaction with the server is made, at the end when PHP spits out the output - everything is destoryed unless saved to a file (session or cookie)

  3. #3
    SitePoint Evangelist catweasel's Avatar
    Join Date
    Apr 2007
    Location
    Goldfields, VIC, Australia
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In other words.. if you want your object to persist over different page loads you need to store it in a $_SESSION variable and retrieve it at the start of each page... otherwise you get a brand spanking new instance of the class for each page.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Objects, like any other variables, only exist until the process ends. PHP scripts are very short-lived, since they only exist in the time from a request goes in, and until the response goes out.

  5. #5
    SitePoint Addict Wildhoney's Avatar
    Join Date
    Apr 2006
    Location
    Nottingham
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Adding onto that even further, your member variables in your class are independently valid for each single instance. You may clone your classes and retain certain values your member variables, but that's not for here.

    So for example, if you were to create 2 instances of a class. You could set the member variables in both instances differently and they wouldn't interfere with one another.

    As aforementioned, if you want to go against the innate nature of the Interweb, then sessions allows you to store data across page reloads based on, typically, a cookie being stored with a unique ID contained within it.
    TalkPHP.com - The Friendly PHP Community

    Watch Reaper Online - Watch Chuck Online

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoops!

    Well, I was expecting my instance of my object to disappear, but there it was, only empty. So I thought there must have been yet another hole in my understanding of OOP theory.

    Turns out, there was a goofy line of code (stuck in there by someone else, btw) that I didn't see.
    So the weird unexplained behaviour isn't so weird after all. Just stupid.
    But I guess I still needed the explanation to know just how to look for oddballs!
    Last edited by cereal_girl; Jan 8, 2008 at 18:09. Reason: I am an idiot!


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
  •