php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #7646 Objects inside session variables get lost
Submitted: 2000-11-05 07:45 UTC Modified: 2000-11-16 04:19 UTC
From: martijnklingens at netscape dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.3pl1 OS: Linux 2.2 / Apache 1.3
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: martijnklingens at netscape dot net
New email:
PHP Version: OS:

 

 [2000-11-05 07:45 UTC] martijnklingens at netscape dot 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...\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>");
?>

Patches

Pull Requests

 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Jul 15 15:00:02 2026 UTC