Re: When is __destruct called on an object in $_SESSION ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/5/14 Peter Ford <pete@xxxxxxxxxxxxx>:
> 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?

The destructor will be called at the end of each page request because
the object in memory is destroyed.

When the object is serialized you will get __sleep being called, and
when it's unserialized you'll get __wakeup.

There is no way to detect when a session is destroyed unless you
implement your own session handler.

-Stuart

-- 
http://stut.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux