|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-07-24 22:43 UTC] fixxxer at php5 dot ru
Description:
------------
The destructor is called if throwing an exception from the constructor. This seems at least illogical and it's contrary to usual behaviour of alike languages like C++ where destructor is not called in this case.
Reproduce code:
---------------
<?
class foo {
function __construct() {
echo "Inside constructor\n";
throw new Exception;
}
function __destruct() {
echo "Inside destructor\n";
}
}
try {
$bar = new foo;
} catch(Exception $exc) {
echo "Caught exception!\n";
}
?>
Expected result:
----------------
Inside constructor
Caught exception!
Actual result:
--------------
Inside constructor
Inside destructor
Caught exception!
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 12:00:01 2025 UTC |
This bug is still reproducible in 5.4 with this code: <?php function throwme($arg) { throw new Exception; } class foo { function __construct() { echo "Inside constructor\n"; throwme($this); } function __destruct() { echo "Inside destructor\n"; } } try { $bar = new foo; } catch(Exception $exc) { echo "Caught exception!\n"; } This produces: Inside constructor Caught exception! Inside destructor This is because the object is kept as part of "args" in the exception's backtrace. Better solution is needed here, the one in f5cf052225ff4bc5ba7fe2c8b9f0ffcd980cac6f does not solve the whole issue.How come that calling exit instead of throwing an exception still produces the old behavior? <?php class A { function __construct() { exit; echo "not reached\n"; } function __destruct() { echo "fail\n"; } } new A;