#42617 [Ver->Bgs]: Destructor not getting called when reference is overwritten

From: Date: Mon, 12 Nov 2007 11:47:43 +0000
Subject: #42617 [Ver->Bgs]: Destructor not getting called when reference is overwritten
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-119536@lists.php.net to get a copy of this message
ID: 42617 Updated by: colder@php.net Reported By: garethinwales at gmail dot com -Status: Verified +Status: Bogus Bug Type: Scripting Engine problem Operating System: CentOS 5 PHP Version: 5.3CVS-2007-10-22 New Comment: I can't reproduce it with the following code: <?php class A { public $name = ''; public function __construct($name) { $this->name = $name; echo "__construct($name)\n"; } public function __destruct() { echo "__destruct($this->name)\n"; } } $a = new A('one'); $a = new A('two'); echo "end\n"; ?> The result is : __construct(one) __construct(two) __destruct(one) end __destruct(two) which seems expected as 1) new A('two'); is evaluated, instantiating the class, 2) assigned to $a, destroying the old value. Previous Comments: ------------------------------------------------------------------------ [2007-10-22 11:22:06] jani@php.net Simplified code: <?php class one { function __construct() { echo "__construct()\n"; } function __destruct() { echo "__destruct()\n"; } } $a = new one; // $a = null; $a = new one; ?> Outputs: __construct() __construct() __destruct() __destruct() And when the $a = null; line is uncommented: __construct() __destruct() __construct() __destruct() ------------------------------------------------------------------------ [2007-09-10 19:22:17] garethinwales at gmail dot com Description: ------------ When the class member $one of class two is replaced by a new object instance, the destructor is not called. This seems incorrect as the previous object is being remove with no references and therefore the destructor should be called. you can get what I'd assume to be the currect fucntionality by uncommenting the line // $this->one=null; Many thanks Gareth Jones Reproduce code: --------------- class one { function __construct() { echo "__construct() one\n"; } function __destruct() { echo "__destruct() one\n"; } } class two { public $one = null; function __construct() { echo "__construct() two\n"; $this->one = new one(); // $this->one=null; $this->one = new one(); } function __destruct() { echo "__destruct() two\n"; } } $two = new two(); Expected result: ---------------- __construct() two __construct() one __destruct() one __construct() one __destruct() two __destruct() one Actual result: -------------- __construct() two __construct() one __construct() one __destruct() one __destruct() two __destruct() one ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=42617&edit=1

« previous php.bugs (#119536) next »