From:
Operating system: Windows 7
PHP version: 5.3.4
Package: Class/Object related
Bug Type: Bug
Bug description:Incorrect call to destructor when object stored in a session
Description:
------------
When an object is stored in the $_SESSION variable, the destructor is still
called at the end of the script. When the object is retrieved from the
$_SESSION on the next invocation, the constructor is not called, but the
destructor is again called.
To duplicate, place the attached code on a web server and load it. Then
press the reload button multiple times to see the results.
Test script:
---------------
<?php
class test {
private $construct_count = 0;
private $destruct_count = 0;
public function __construct() {
$this->construct_count++;
echo "Constructor called.<br>\n";
}
public function __destruct() {
$this->destruct_count++;
echo "Destructor called.<br>\n";
}
}
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Test Script</title>
</head>
<body>
<?php
if (!isset($_SESSION['test'])){
echo "Session not found<br>\n";
$_SESSION['test'] = new Test();
}
else
echo "Session found<br>\n";
echo "<pre>\n";
print_r($_SESSION);
echo "</pre>\n";
?>
</body>
</html>
Expected result:
----------------
Session not found
Constructor called.
Array
(
[test] => test Object
(
[construct_count:test:private] => 1
[destruct_count:test:private] => 0
)
)
After second and subsequent invocations:
Session found
Array
(
[test] => test Object
(
[construct_count:test:private] => 1
[destruct_count:test:private] => 0
)
)
Actual result:
--------------
After first invocation:
Session not found
Constructor called.
Array
(
[test] => test Object
(
[construct_count:test:private] => 1
[destruct_count:test:private] => 0
)
)
Destructor called.
After second and subsequent invocations:
Session found
Array
(
[test] => test Object
(
[construct_count:test:private] => 1
[destruct_count:test:private] => 1
)
)
Destructor called.
Note: destruct_called increments on each invocation. construct_count does
not.
--
Edit bug report at http://bugs.php.net/bug.php?id=53534&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=53534&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=53534&r=trysnapshot53
Try a snapshot (trunk):
http://bugs.php.net/fix.php?id=53534&r=trysnapshottrunk
Fixed in SVN:
http://bugs.php.net/fix.php?id=53534&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=53534&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=53534&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=53534&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=53534&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=53534&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=53534&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=53534&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=53534&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=53534&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=53534&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=53534&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=53534&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=53534&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=53534&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=53534&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=53534&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=53534&r=mysqlcfg