Posted: Mon Nov 26, 2007 12:32 pm
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!
Good to know!
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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.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.
That doesn't work for me.It is news that you can now include the class after session_start() but before the object in the is used.
Code: Select all
<?php
session_start();
if ( !isset($_SESSION['foo']) ) {
$_SESSION['foo'] = new Bar;
}
class Bar {
public $xyz;
}
print_r($_SESSION);
?>Code: Select all
<?php
session_start();
if ( !isset($_SESSION['foo']) ) {
$_SESSION['foo'] = new Bar;
}
if ( true ) {
class Bar {
public $xyz;
}
}
print_r($_SESSION);
?>