Bug #53699 [Com]: After destructor execution, object exists
| From: | cmbecker69 at gmx dot de | Date: | Thu, 08 Jan 2015 23:53:58 +0000 |
| Subject: | Bug #53699 [Com]: After destructor execution, object exists | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189791@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=53699&edit=1
ID: 53699
Comment by: cmbecker69 at gmx dot de
Reported by: hausner15 at gmail dot com
Summary: After destructor execution, object exists
Status: Open
Type: Bug
Package: Class/Object related
Operating System: *
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Actually, executing the destructor doesn't destroy the object, but
rather the destructor is called by the PHP engine before the
object is destroyed (i.e. its memory is released). However, PHP
makes no guarantee that the destructor is called *immediately*
before destroying the object.
Consider the following script with a cyclic reference between $a
and $b:
<?php
class A {
function __destruct() {
$this->b->tell();
}
function tell() {
echo "I'm an instance of A\n";
}
}
class B {
function __destruct() {
$this->a->tell();
}
function tell() {
echo "I'm an instance of B\n";
}
}
$a = new A();
$b = new B();
$a->b = $b;
$b->a = $a;
Neither object can't be destroyed as long as the other is alive.
To be able to fulfill the contract that all destructors are
called, the engine has to call both destructors first, and only
then can destroy both objects.
Even if there is no cyclic reference, as in the test script of the
OP, detecting that during the shutdown sequence is expensive (if
possible at all), so the engine calls all destructors (in
undefined order[1]), before destroying the objects.
So, IMHO, this is not a bug, but rather a WONTFIX.
[1] <http://php.net/manual/en/language.oop5.decon.php#language.oop5.decon.destructor>
Previous Comments:
------------------------------------------------------------------------
[2011-01-08 15:59:41] hausner15 at gmail dot com
Description:
------------
I think, in this example after destructor executing, object shouldn`t exist. But result of this code
tells something else...
Test script:
---------------
<?php
class A {
protected $var;
private static $instance = NULL;
public static function getInstance() {
if (is_null(self::$instance))
self::$instance = new A();
return self::$instance;
}
protected function __construct() {
echo "creating instance of A\n";
$this->var = "existing var in instance of A \n";
}
public function show() {
echo $this->var;
}
public function __destruct() {
echo "deleting instance of A\n";
}
}
class B {
private static $instance = NULL;
public static function getInstance() {
if (is_null(self::$instance))
self::$instance = new B();
return self::$instance;
}
protected function __construct() {
echo "creating instance of B\n";
}
public function __destruct() {
A::getInstance()->show();
echo "deleting instance of B\n";
}
}
header('Content-type: text/plain');
A::getInstance()->show();
B::getInstance();
?>
Expected result:
----------------
Some error?
Actual result:
--------------
creating instance of A
existing var in instance of A
creating instance of B
deleting instance of A
existing var in instance of A
deleting instance of B
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=53699&edit=1