Storing an object in a session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I knew it worked without manually un/serializing(), but it's news to me that you can define your class after the session_start()

Good to know!
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I'm not sure, but I tend to think that there are relatively big changes between old and new PHP versions and the PHP site doesn't announce about them out and loud enough as they should have :?
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Jcart wrote:Well I think it is obvious that there will be overhead from having to persist objects in sessions and [s]serializing/unserializing[/s], but I would probably avoid its usage simply because it smells of a poor design. I would probably prefer storing the data and not the object itself in the session, then simply repopulate it on the next request.
If the class is designed to store only data you intend on saving (like state information), you're saving overhead by storing the object itself in the session. I'd do some microbenchmarks, but I'm sure that PHP will serialize/unserialize an object faster than it will extract and reassign each object property to a variable, serialize each variable, then undo the whole process. The extra steps save nothing, lose some cycles and add extra, unnecessary code.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I can think of several cases where I store objects in the session. Probably the most common with web applications is a Shopping Cart which is usually an object that contains one or more item objects.

It is news that you can now include the class after session_start() but before the object in the is used. It sounds like they may have moved the unserialize and object/class fixup from within session_start() to when the var is first accessed. It makes sense to lazy initialize because session vars are not necessarily used on every page that starts the session.
(#10850)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It is news that you can now include the class after session_start() but before the object in the is used.
That doesn't work for me.

Code: Select all

<?php
session_start();
if ( !isset($_SESSION['foo']) ) {
	$_SESSION['foo'] = new Bar;
}

class Bar {
	public $xyz;
}

print_r($_SESSION);
?>
That is working. But the class definition is present before session_start() is called because all unconditional class definition in a script file are parsed and registered before the script is executed. But try

Code: Select all

<?php
session_start();
if ( !isset($_SESSION['foo']) ) {
	$_SESSION['foo'] = new Bar;
}

if ( true ) {
class Bar {
	public $xyz;
}
}
print_r($_SESSION);
?>
and you'll get a __PHP_Incomplete_Class object.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Which makes lazy loading of classes that are used in the session (without __autoload) tricky. You can obviously also get around the problem by delaying when you do session_start(). I do the latter by having my session object only start the session when you first ask for data from it.
(#10850)
Post Reply