Edit report at http://bugs.php.net/bug.php?id=53534&edit=1
ID: 53534 Updated by: [email protected] Reported by: jstuckle at attglobal dot net Summary: Incorrect call to destructor when object stored in a session -Status: Open +Status: Bogus Type: Bug Package: Class/Object related Operating System: Windows 7 PHP Version: 5.3.4 Block user comment: N Private report: N New Comment: Expected behavior. serialize(), unserialize(), __sleep(), __wakeup() http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.sleep http://php.net/serialize http://php.net/unserialize Previous Comments: ------------------------------------------------------------------------ [2010-12-13 01:57:16] jstuckle at attglobal dot net 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 this bug report at http://bugs.php.net/bug.php?id=53534&edit=1
