php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31700 explicit destruction of an object not possible
Submitted: 2005-01-26 02:51 UTC Modified: 2010-12-20 11:48 UTC
From: attibln at gmx dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: attibln at gmx dot net
New email:
PHP Version: OS:

 

 [2005-01-26 02:51 UTC] attibln at gmx dot net
Description:
------------
On second call of __destruct() there should be an error message like "object not existing".

(http://www.php.net/manual/de/language.oop5.decon.php)
"PHP 5 introduces a destructor concept <b>similar to that of other object-oriented languages, such as Java</b>. The destructor method will be called as soon as all references to a paticular object are removed or when the object is <b>explicitly destroyed</b>."

My system:
gentoo 2.4.26-gentoo-r9
apache 2.0.51-r1
PHP Version 5.0.3 / Zend Engine v2.0.3

(php) use flags:
-adabas +apache2 -bcmath -berkdb -birdstep +bzlib -calendar -cdb -cpdflib +crypt -ctype +curl +curlwrappers -db2 -dba -dbase -dbm -dbmaker -dbx -debug -dio -empress -empress-bcs -esoob +exif -fam -fdftk -filepro -flatfile -frontbase -ftp +gd -gd-external -gdbm -gmp -hyperwave-api -iconv -imap -informix -ingres -inifile -interbase -iodbc +jpeg -kerberos -ldap -libedit -mcve +memlimit -mhash +mime -ming -mnogosearch -msession -msql -mssql +mysql -mysqli +ncurses -nis +nls -oci8 -odbc -oracle7 -ovrimos +pcntl -pcre -pfpro +png -posix +postgres -qdbm -readline -recode -sapdb -sasl +session -shared -sharedmem +simplexml -snmp +soap +sockets -solid -spell +spl -sqlite +ssl -sybase -sybase-ct -sysvipc +tidy -tiff +tokenizer -truetype -wddx +xml2 -xmlrpc -xpm +xsl +zlib

Reproduce code:
---------------
<?php

class MyDestructableClass {
  function __construct() {
    print "<br />In constructor\n";
    $this->name = "MyDestructableClass";
  }

  function __destruct() {
    print "<br />Destroying " . $this->name . "\n";
  }
}

$obj = new MyDestructableClass();
print( '<br />before destruction' );
$obj->__destruct();
$obj->__destruct();
$obj->__destruct();
print( '<br />before script end' );

?>


Expected result:
----------------
something like:

Fatal error: ... Destruction of not existing object ist not possible! ... in ... on line ...

Actual result:
--------------
Explicit destruction "not possible".
In other words:
Calling __destruct on an object is possible as often it is done.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-26 09:05 UTC] derick@php.net
This is expected. To destroy an object use: unset($obj);
 [2005-01-26 20:12 UTC] attibln at gmx dot net
Well I do not really want to do nitpicking, but why is it called __destruct() if it does _not_destruct_?! Wouldn't be __exit_method() and __start_method() better choosen then?!

As #31701 this behaviour is not real object oriented behaviour. In my opinion the engine should unset an object when __destruct() is called - similar as it's done in other OOL like C++.

To my surprise unset($obj) really executes __destruct(). This should be mentioned in the _documentation_ "Classes & Objects (PHP5) Constructor & Destructor". Additionally advising not to call __destruct() explicitly but using unset() - as it's stated to use keyword new to create an object instead of __construct(). An example:

<?php

class MyDestructableClass {
  function __construct() {
    print "<br />In constructor\n";
    $this->name = "MyDestructableClass";
  }

  function __destruct() {
    print "<br />Destroying " . $this->name . "\n";
  }
}

$obj = new MyDestructableClass();
unset( $obj );

?>
 [2005-01-26 20:23 UTC] attibln at gmx dot net
Well I'm not quite sure if finishing the script or unset() executes __destruct(), although I rather guess it's finishing the script.

In that case ... well ... all I can say I dislike that behaviour then. Destruct should destruct!
 [2005-01-26 20:48 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

In a garbage collected language like PHP it is impossible to forcefully destruct an instance. If you need that use c++ or other languages that don't have garbage collectors.
 [2010-12-20 11:48 UTC] jani@php.net
-Package: Tidy +Package: Scripting Engine problem
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 27 11:00:03 2025 UTC