n3p3 Posted February 12, 2008 Share Posted February 12, 2008 When an object reference is passed to the $_SESSION, what happens to the object? For ex, in login system, user first enters the username and password, (assuming its correct) defining the session variable as a reference to the database object like $_SESSION["db_cnn"]=&new DB(...); and accessing to db object as $_SESSION["db_cnn"]->Query(...) in other pages... is it an optimized way to use sessions in this purpose? can it be implemented to the MVC pattern? What are the advantages, and disadvantages? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/90730-passing-object-reference-to-a-session/ Share on other sites More sharing options...
trq Posted February 12, 2008 Share Posted February 12, 2008 Objects are actually serialized when put into the session array, so... $_SESSION["db_cnn"]->Query(...) wont work as far as I know. Quote Link to comment https://forums.phpfreaks.com/topic/90730-passing-object-reference-to-a-session/#findComment-465063 Share on other sites More sharing options...
448191 Posted February 12, 2008 Share Posted February 12, 2008 Indeed resources cannot be serialized. If you want to serialize a database connection object, use __wakeup() to restore the connection upon unserialization. Quote Link to comment https://forums.phpfreaks.com/topic/90730-passing-object-reference-to-a-session/#findComment-465124 Share on other sites More sharing options...
n3p3 Posted February 12, 2008 Author Share Posted February 12, 2008 actually it works..a simple example (referencing the example in php.net/autoload), autoload.php <?php function __autoload($class) { require_once("$class.php"); } ?> sampleClass.php <?php class sampleClass { function __construct($value) { $this->dbvar = $value; } function getValue() { return $this->dbvar; } } ?> page1.php <?php require_once('autoload.php'); session_start(); $_SESSION['testObj'] = new sampleClass('localhost'); echo '<a href="page2.php">Go to page 2</a>'; ?> page2.php <?php require_once('autoload.php'); session_start(); echo $_SESSION['testObj']->getValue(); ?> But..When an object reference is passed to the $_SESSION, what happens to the object? If the object is still allocated in the memory, instead of creating a new object in page2.php, we can access the object by using object reference stored in our session variable..and I believe it will be a good optimization. Am I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/90730-passing-object-reference-to-a-session/#findComment-465151 Share on other sites More sharing options...
448191 Posted February 12, 2008 Share Posted February 12, 2008 actually it works..a simple example (referencing the example in php.net/autoload), No it doesn't, we're just not talking about the same thing. Objects can be serialized (thank god), resources can not. And no, using the default php session handler objects put in session are not kept in memory. The session extension creates a string presentation of them and writes them to disk. Plenty of other possibilities, but all of them serialize (in php that by means flattening them to a string) the objects. Except for one option, but you don't want to go there. Quote Link to comment https://forums.phpfreaks.com/topic/90730-passing-object-reference-to-a-session/#findComment-465194 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.