SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Mar 2007
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Objects question

    I have recently been looking into PHP and I have a question about objects for you all. Do objects stay alive between pages? Or do you need to create any objects you require for each page. I am wondering as php is interpreted and is called with the file as an arg (I think). So if this is the case how do you create objects that can stay alive as long as the user is on your site similar to sessions.

    Pointers to tutorials and articles welcomed.

  2. #2
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sessions are really the way to go here.

    you could use serialize to store the object.

    If the cookies are causing a problem, you could remember the user with url based sessions, or by storing their ip adress along with the serialized object in a databse or flat file system.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2007
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastically fast response - thanks.

    So an object does usually die after the php file has been parsed. If I wanted an object to be persitent across a session I would have to store it (or at least its properties) along with the session some how.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $s serialize(new object); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Addict
    Join Date
    Mar 2007
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    PHP Code:
    $s serialize(new object); 
    Can you then store $s as a session variable in a database.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    aye or you can put it directly into a session var.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Addict
    Join Date
    Mar 2007
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

  8. #8
    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)
    You generally don't need to do that with PHP. The less you put in session, the better your application is. (As a rule of thumb at least). If you're worried about performance -- don't. Sessions are slower than instantiating the objects as needed anyway.

  9. #9
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    start_session();
    $_SESSION["anobject"] = serialize( $object );
    then in the next page:
    Code:
    start_session();
    $object = unserialize($_SESSION["anobject"]);

  10. #10
    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)
    Quote Originally Posted by Php_penguin View Post
    Code:
    start_session();
    $_SESSION["anobject"] = serialize( $object );
    then in the next page:
    Code:
    start_session();
    $object = unserialize($_SESSION["anobject"]);
    You don't need to serialize/unserialize the object. That will happen automatically by the session-handler. Just use $_SESSION as a regular array.

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is a thread about object persistence here, you may want to take a look:
    http://www.sitepoint.com/forums/showthread.php?t=468429

  12. #12
    SitePoint Addict
    Join Date
    Mar 2007
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by libranet View Post
    There is a thread about object persistence here, you may want to take a look:
    http://www.sitepoint.com/forums/showthread.php?t=468429
    Thanks libranet that was an interesting read.

  13. #13
    SitePoint Addict Php_penguin's Avatar
    Join Date
    Aug 2004
    Location
    Colwyn Bay, Wales, UK
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Storing the object as an array straight into a session means more cookies are created doesn't it? Which leads to the load time being slower?

    I'm just pretty sure that serializing the array will improve performance.

  14. #14
    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)
    Quote Originally Posted by Php_penguin View Post
    Storing the object as an array straight into a session means more cookies are created doesn't it? Which leads to the load time being slower?
    No. There is one cookie for the whole session, and what you stores in the session, doesn't affect the size of the cookie. The cookie just contains an id, which is unique for the visitor.

    Quote Originally Posted by Php_penguin View Post
    I'm just pretty sure that serializing the array will improve performance.
    That is not the case. If you serialize the object manually, it will end up getting serialized twice -- First the object is (by you) serialized into a string. Then the session-handler will serialize this string into another string. The reverse happens on unserialization.

    Nonetheless, performance should not really have anything to say in this, since you should never store so much information in the session, that it matters.


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
  •