Re: Saving variables in a session in a destructor

From: Date: Wed, 30 Jun 2004 08:10:29 +0000
Subject: Re: Saving variables in a session in a destructor
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-189273@lists.php.net to get a copy of this message
Maybe you check out the internals archives as an discussion about this topic was held 1-2 days ago. > 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 (#189273) next »