php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24637 __destruct not called
Submitted: 2003-07-13 15:33 UTC Modified: 2003-08-10 19:03 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: Bertrand dot Willm at laposte dot net Assigned: helly (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.0.0b1 (beta1) OS:
Private report: No CVE-ID: None
 [2003-07-13 15:33 UTC] Bertrand dot Willm at laposte dot net
Description:
------------
There is no recursive call to __destruct when it is not defined in a derived class.
There is not this problem with __construct.
This can be dangerous be cause if a destructor is declared, all the derived clas have to declare a __destruct to be sure that the destruction is done.
In other way __destruct is obligatory not to have problem, but this is not flexible.

Reproduce code:
---------------
class CBaseClass {
   var $name;
   function __construct($name) {
      $this->name = $name;
      echo "$this->name-&gt;CBaseClass::__construct()<br>";
   }
   
   function __destruct() {
      echo "$this->name-&gt;CBaseClass::__destruct()<br>";
   }
}

class CExtClass extends CBaseClass {
}

echo 'Creation of ExtObject<br>';
$ExtObject = new CExtClass('ExtObject');

echo 'End of script<br>';


Expected result:
----------------
Creation of ExtObject
ExtObject->CBaseClass::__construct()
End of script


Actual result:
--------------
Creation of ExtObject
ExtObject->CBaseClass::__construct()
End of script
ExtObject->CBaseClass::__destruct()


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-14 14:20 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The instance is destructed of course after script termination if the script doesn't destroy it itself. And when a derived class has no specialized destructor then of course the inherited destructor is to be called.
 [2003-07-14 15:24 UTC] Bertrand dot Willm at laposte dot net
I probably make an error entering the bug.
What should be read is:

Actual result:
--------------
Creation of ExtObject
ExtObject->CBaseClass::__construct()
End of script

Expected result:
----------------
Creation of ExtObject
ExtObject->CBaseClass::__construct()
End of script
ExtObject->CBaseClass::__destruct()
 [2003-07-14 15:29 UTC] helly@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2003-07-16 01:04 UTC] Bertrand dot Willm at laposte dot net
I did not see any difference with this new release.
 [2003-07-16 02:52 UTC] helly@php.net
Try this simple CLI command:
php -n -r 'class t { function __destruct() { echo "Bla\n";} } $o= new t;'

For me it outputs Bla what means the destructor is called during shutdown. However it is possible that one of my pending patches causes the correct behavior. Please verify again.
 [2003-07-22 12:47 UTC] Bertrand dot Willm at laposte dot net
On PHP 5.0.0b2-dev (cgi-fcgi) (built: Jul 14 2003 20:08:40)
there is no -r option.
I try this:
php -n
and on the standard input:
<? class t { function __destruct() { echo "Bla\n";} } $o= new t; ?>
This should be equivalent. It works.
What does not work is the following:
php -n -r '<? class t { function __destruct() { echo "Bla\n";} } class u extends t {} $o= new u; ?>'
As there is no -r on my php I enterred this:
php -n
and in the standard input:
<? class t { function __destruct() { echo "Bla\n";} } class u extends t {} $o= new u; ?>
This does not work.
 [2003-07-22 13:55 UTC] helly@php.net
-r is only available in CLI and there you do not need '<?php ?>' also when you use '<? ?>' this has to be enabled by ini settings.

Anyway. At the moment constructors/destructors are inherited as functions only and not as constructors/destructors. I guess the usual suspects have to discuss that a bit :-)

Thanks for notifying
 [2003-08-10 19:03 UTC] helly@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 05 11:01:28 2024 UTC