Bug #53699 [Opn->Nab]: After destructor execution, object exists

From: Date: Thu, 16 Apr 2015 18:59:20 +0000
Subject: Bug #53699 [Opn->Nab]: After destructor execution, object exists
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192139@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 Updated by: cmb@php.net Reported by: hausner15 at gmail dot com Summary: After destructor execution, object exists -Status: Open +Status: Not a bug Type: Bug Package: Class/Object related Operating System: * PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: Sorry, but your problem does not imply a bug in PHP itself, as explained in the comment above. Thank you for your interest in PHP. Previous Comments: ------------------------------------------------------------------------ [2015-01-08 23:53:57] cmbecker69 at gmx dot de 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> ------------------------------------------------------------------------ [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

« previous php.bugs (#192139) next »