Page 2 of 2

Posted: Mon Nov 26, 2007 12:32 pm
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!

Posted: Mon Nov 26, 2007 1:09 pm
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 :?

Posted: Mon Nov 26, 2007 7:14 pm
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.

Posted: Mon Nov 26, 2007 7:37 pm
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.

Posted: Tue Nov 27, 2007 5:05 am
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.

Posted: Tue Nov 27, 2007 11:33 am
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.