PHP 4.0 Bug #7646: Objects inside session variables get lost

From: Date: Sun, 05 Nov 2000 12:45:14 +0000
Subject: PHP 4.0 Bug #7646: Objects inside session variables get lost
Groups: php.dev 
Request: Send a blank email to php-dev+get-37176@lists.php.net to get a copy of this message
From: martijnklingens@netscape.net Operating system: Linux 2.2 / Apache 1.3 PHP version: 4.0.3pl1 PHP Bug Type: Scripting Engine problem Bug description: Objects inside session variables get lost Hi, I encountered a strange bug in PHP when a session variable (an object in my case) holds instances of other classes. As soon as I call a method on such an embedded instance, the instance is lost the next time a page is loaded. I guess the garbage collector is a little bit too good and cleans up objects that are still in use, but I'm not completely sure about that. _READING_ a variable (as opposed to calling methods) does not trigger the bug, so a workaround is copying the object and calling the method on the copy, but I guess it should be fixed nevertheless. Below is a script that reliably reproduces the bug on my system. It's rather large, but with all the echo statements it should be immediately clear what it does. If you still have questions about the script or about my configuration, feel free to drop me an email at martijnklingens@netscape.net Thanks in advance for your help, Martijn Klingens Script Below ------> <?PHP // Class A is the session var that holds instances of class B class A { var $Items = array (); function A () { $this->Items [] = new B ("B1"); $this->Items [] = new B ("B2"); } } class B { var $Value; function B ($Value) { $this->Value = $Value; } function SomeFunction () { // Just do nothing, calling the method is enough to trigger the bug } } session_register ("MyA"); session_register ("Count"); echo ("<html><body><pre>"); // Create A if it doesn't exist: if (!isset ($MyA)) { echo ("Creating session and instance of MyA...\n\n"); $MyA = new A; $Count = 1; // Current Step echo ("Dump of MyA:\n"); print_r ($MyA); echo ("\n<a href=$PHP_SELF>Go to step 2</a>\n"); } else { // Session is registered already, call method on class B: $Count++; echo ("Session is already registered\n"); echo ("You are now in step $Count. MyA holds:\n"); print_r ($MyA); if ($Count == 2) // Step 2, i.e. trigger the bug? { echo ("\nCalling method on MyA->Items [0].\n"); $MyA->Items [0]->SomeFunction (); echo ("Done. MyA now still holds the same data:\n"); print_r ($MyA); echo ("\nIf you go to the next step, you'll notice that the Item\n"); echo ("on which we just called our method got lost when the session\n"); echo ("variable was stored by PHP!\n"); echo ("<a href=$PHP_SELF>Go to the last step</a>"); } else echo ("Notice that MyA->Items [0] no longer exists!"); } echo ("</pre></body></html>"); ?> -- Edit Bug report at: http://bugs.php.net/?id=7646&edit=1

« previous php.dev (#37176) next »