/usr/portage

Shutdown sequence and session handling 0

Evert: One thing you should be aware of is, that the actual process of collecting the garbage (and freeing the memory) is done after the session handler functions have been called. This means as long as you don’t unset anything important for your session handling in one of your destructors you can even do it without registering your session close function as a shutdown function.
The following snippet demonstrates this pretty well:

<?php
class SessionHandler
{
    protected $_resource;
    public function __construct()
    {
        $this->_resource = fopen('/tmp/foo.txt', 'w+');
    }
    public function session_write()
    {
        echo __METHOD__ . "(): " . $this->_checkResource();
    }
    public function session_close()
    {
        echo __METHOD__ . "(): " . $this->_checkResource();
    }
    protected function _checkResource()
    {
        return "Resource exists: " . (is_resource($this->_resource) ? 'true' : 'false') . "\n";
    }
}
$handler = new SessionHandler();
$anon = create_function('', '');
session_set_save_handler($anon, array($handler, 'session_close'), $anon, array($handler, 'session_write'), $anon, $anon);
session_start();

... and the output:

$ php sess.php
SessionHandler::session_write(): Resource exists: true
SessionHandler::session_close(): Resource exists: true

Filed under & no comments & no trackbacks

Trackbacks

Trackback specific URI for this entry

No Trackbacks

Comments

No comments

Add a Comment & let me know what you think