|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-07-06 16:58 UTC] antonr at game dot permonline dot ru
Description:
------------
Session are closed before all object destructors executed. It seems to be wrong, cause objects desctructors may save information in opened session.
Reproduce code:
---------------
<?php
class MySession
{ public $number;
public function __construct()
{ session_start();
$this->number = @$_SESSION["number"];
}
public function Process()
{ $this->number++;
}
public function __destruct()
{ $_SESSION["number"] = $this->number;
session_write_close();
}
}
$ss = new MySession();
$ss->Process();
echo $ss->number;
?>
Expected result:
----------------
With every page reloading number in the page must increase by 1.
Actual result:
--------------
Everytime number is equal 1.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 01:00:02 2025 UTC |
Modifying $_SESSION in the destructor ONLY works if you call unset($object) before the end of your script, however this is a pain in the rear - and if you have references to that object elsewhere it doesn't work properly. There is a workaround... You CAN use register_shutdown_function in the constructor of your class public function __construct() { register_shutdown_function(array($this, '__destruct'); } and then the destruct is called early enough that you can modify $_SESSION. But this defeats the whole purpose of a destructor - I could do that in every class if php5 didn't support destructors (in fact I did in php4) but I thought the point was to eliminate messy workarounds. Now why in the world the sessions are closed in between when shutdown functions and destructors are called is beyond me.