Saving variables in a session in a destructor

From: Date: Wed, 30 Jun 2004 03:41:49 +0000
Subject: Saving variables in a session in a destructor
Groups: php.general 
Request: Send a blank email to php-general+get-189268@lists.php.net to get a copy of this message
Hi all Using PHP5, I am trying to save some properties of an object when it is destroyed in its destructor, __destruct(). However, I have found that the session variable is NOT stored unless I explicitly destroy the object using unset(). If I leave php to finish executing the script and automatically destroy the object, the destructor IS called, however, the session variables are NOT saved. A quick code example for clarity: ----- class StateMachine { public $stateVariable; function __destruct() { $_SESSION['state'] = $this->stateVariable; } } $sm = new StateMachine(); if (isset($_SESSION['state'])) { $sm->stateVariable = $_SESSION['state']; } else { $sm->stateVariable = 'foobar'; } ---- (please ignore the obvious bad coding standard of making that var public and accessing it, this is for simplicity of the example). Unless I do an unset($sm); at the end of a script like this, the $_SESSION array will never contain the state=>foobar key/value. Can anyone offer some insight into this? Thanks! Dave

« previous php.general (#189268) next »