SitePoint Sponsor |
|
User Tag List
Results 1 to 14 of 14
Thread: PHP Objects question
-
Jun 19, 2007, 04:38 #1
- 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.
-
Jun 19, 2007, 04:43 #2
- 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.
-
Jun 19, 2007, 04:52 #3
- 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.
-
Jun 19, 2007, 05:08 #4PHP Code:
$s = serialize(new object);
-
Jun 19, 2007, 05:18 #5
- Join Date
- Mar 2007
- Posts
- 286
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 19, 2007, 05:26 #6
aye or you can put it directly into a session var.
-
Jun 19, 2007, 05:39 #7
- Join Date
- Mar 2007
- Posts
- 286
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks.
-
Jun 19, 2007, 05:46 #8
- 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.
-
Jun 19, 2007, 10:01 #9
- 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 );
Code:start_session(); $object = unserialize($_SESSION["anobject"]);
-
Jun 19, 2007, 10:36 #10
- 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
-
Jun 19, 2007, 12:29 #11
- Join Date
- Mar 2007
- Posts
- 286
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 19, 2007, 14:26 #12
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Jun 20, 2007, 07:25 #13
- 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.
-
Jun 20, 2007, 08:32 #14
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
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