Re: PHP 4.0 Bug #7646 Updated: Objects inside session variables get lost
| From: | Jani Taskinen | Date: | Thu, 16 Nov 2000 09:26:32 +0000 |
| Subject: | Re: PHP 4.0 Bug #7646 Updated: Objects inside session variables get lost | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-38312@lists.php.net to get a copy of this message | ||
Why was this closed? No comment?
--Jani
On 16 Nov 2000 onn@php.net wrote:
>ID: 7646
>Updated by: onn
>Reported By: martijnklingens@netscape.net
>Status: Closed
>Bug Type: Scripting Engine problem
>Assigned To:
>Comments:
>
>
>
>Previous Comments:
>---------------------------------------------------------------------------
>
>[2000-11-05 07:45:14] martijnklingens@netscape.net
>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...nn");
> $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 registeredn");
> 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 Itemn");
> echo ("on which we just called our method got lost when the sessionn");
> 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>");
>?>
>
>
>---------------------------------------------------------------------------
>
>
>Full Bug description available at: http://bugs.php.net/?id=7646
>
>
>