When is __destruct called on an object in $_SESSION ?

From: Date: Thu, 14 May 2009 11:29:47 +0000
Subject: When is __destruct called on an object in $_SESSION ?
Groups: php.general 
Request: Send a blank email to php-general+get-292566@lists.php.net to get a copy of this message
I'm sure I've seen something about this before, but I can't find it: I'm creating a file which needs to live for the duration of a session, and ONLY the duration of the session. I made a little call which holds the name of the file like this: <?php class __TFR { private $file; public function __construct() { $this->file = '/tmp/'.session_id().'.xml'; } public function __get($name) { if ($name=='file') return $this->file; } public function __destruct() { @unlink($this->file); } } ?> So I create an instance of this object and I put a reference to the object in the session: <?php $_SESSION['TFR'] = new __TFR(); ?> I was then expecting TFR::__destruct() to only be called when the session was closed, with either a timeout, or a session_destroy() call. But it looks like the object destructor is called at the end of every page. Any ideas about working around that? -- Peter Ford phone: 01580 893333 Developer fax: 01580 893399 Justcroft International Ltd., Staplehurst, Kent

« previous php.general (#292566) next »