note 62260 added to function.session-set-save-handler

From: Date: Thu, 23 Feb 2006 15:09:16 +0000
Subject: note 62260 added to function.session-set-save-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-104304@lists.php.net to get a copy of this message
In regard to the note dated 28-Jan-2006 09:15 (there is no email) and the webpage it links to. That is not the way objects should be saved into session. You should not just assign the entire object to $_SESSION['myObj'] and hope for the best while waiting for the script to end and for the php internal engine to serialize your object, call destructors etc. You should serialize your object beforehand by specifically calling serialize() on your object and assign the serialized string to a session key. Yeah, You might be wondering if this isn't defeating the whole purpose. It's not. This way you are sure that your object is serialized correctly and don't have to worry about how session handlers mess with it. You now have a string variable to assign wherever you want and you avoid all the mess of session write()/close() being called before object destructors. You also have more control over the state of the object you want to save into session (the serialized object will not reflect changes done to the object after serialization) and will also end up with a code that is more likely to survive over time in case the guys at PHP decide to make other changes to the session functions. Marcus has recently introduced the Serializable interface (php 5.1.2) which IMO will improve things *a lot* and is the right way to serialize objects. The problem with it though is that it breaks object references in some cases. I already made a bug report here: http://bugs.php.net/bug.php?id=36424 You can mimic the Serializable interface's methods by adding a public method to your classes that assigns serialize($this) to $_SESSION['myObj'] and another static method that acts as a constructor, returning unserialize($_SESSION['myObj']). This way you have even more control for serializing your objects as you can modify your Foo::serialize()/unserialize() methods to perform other tasks as well. <?php class Foo { const SESSION_KEY = 'someKeyName'; // ... // other definitions go here // ... static function unserialize ($key = self::SESSION_KEY) { return isset($_SESSION[$key]) ? unserialize($_SESSION[$key]) : false; } public function serialize ($key = self::SESSION_KEY) { $_SESSION[$key] = serialize($this); } } $Obj = new Foo(); // do somehting on $Obj here $Obj->serialize(); // continue until the end of script ?> When getting your object from session you just do: <?php // try getting your obj from session $Obj = Foo::unserialize(); // and to make sure you do a valid Foo instance: // (you can also do this inside Foo::unserialize() if you want) $Obj instanceOf Foo or $Obj = new Foo(); ?> If you need to save multiple instances of the same class then just pass a different parameter to Foo::serialize() and Foo::unserialize(). You can also extend your classes over a class like Foo above and overload the Foo::serialize()/unserialize() methods. Of course, you can do all that without having dedicated methods in your classes (yuck). ---- Server IP: 64.71.164.2 Probable Submitter: 82.231.160.66 ---- Manual Page -- http://www.php.net/manual/en/function.session-set-save-handler.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62260 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62260&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62260&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62260&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62260&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62260&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62260&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62260&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62260&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104304) next »