Edit report at http://bugs.php.net/bug.php?id=53534&edit=1

 ID:                 53534
 User updated by:    jstuckle at attglobal dot net
 Reported by:        jstuckle at attglobal dot net
 Summary:            Incorrect call to destructor when object stored in a
                     session
 Status:             Bogus
 Type:               Bug
 Package:            Class/Object related
-Operating System:   Windows 7
+Operating System:   Irrelevant
-PHP Version:        5.3.4
+PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

Additional information - the version and OS are irrelevant for this bugs
- it occurs in 5.2.x and 5.3.x versions on both Windows and Linux.



Also, the call to the destructor is invalid because the object is not
being destroyed; it is being saved in the session.


Previous Comments:
------------------------------------------------------------------------
[2010-12-13 04:00:43] jstuckle at attglobal dot net

This may be "expected behavior" - but it is a direct violation of OO
operations.  By definition, after a call to the destructor the object is
in an undefined state.  It is invalid to use this object again without
reinitializing it, i.e. calling a constructor.



It is also invalid to call a destructor on an uninitialized object, so
under correct operation, the destructor calls always have a 1-to-1
relationship with constructor calls.

------------------------------------------------------------------------
[2010-12-13 03:24:07] [email protected]

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

------------------------------------------------------------------------
[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

Reply via email to