php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29368 The destructor is called when an exception is thrown from the constructor
Submitted: 2004-07-24 22:43 UTC Modified: 2016-07-13 13:46 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: fixxxer at php5 dot ru Assigned: helly
Status: Closed Package: Scripting Engine problem
PHP Version: 5.4.0 OS: *
Private report: No CVE-ID:
 [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!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-25 19:39 UTC] helly@php.net
This bug has been fixed in CVS.

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/.
 
Thank you for the report, and for helping us make PHP better.


 [2004-09-24 16:43 UTC] ernest at vogelsinger dot at
This bug still (again?) exists in 5.0.2/Win32!
 [2013-01-06 04:14 UTC] stas@php.net
-Status: Closed +Status: Re-Opened -PHP Version: 5.0.0 +PHP Version: 5.4.0
 [2013-01-06 04:14 UTC] stas@php.net
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.
 [2016-07-13 13:44 UTC] dmitry@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=afd3e39d6697e36d3b92cee5da911e4ea0de3901
Log: Fixed bug #29368 (The destructor is called when an exception is thrown from the constructor).
 [2016-07-13 13:44 UTC] dmitry@php.net
-Status: Re-Opened +Status: Closed
 [2016-07-13 13:46 UTC] dmitry@php.net
Fixed in PHP-7.1 only, because the fix causes minor BC break.
http://git.php.net/?p=php-src.git;a=commitdiff;h=afd3e39d6697e36d3b92cee5da911e4ea0de3901
 [2016-07-20 11:29 UTC] davey@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=afd3e39d6697e36d3b92cee5da911e4ea0de3901
Log: Fixed bug #29368 (The destructor is called when an exception is thrown from the constructor).
 [2016-08-22 20:19 UTC] lauri dot kentta at gmail dot com
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;
 [2016-10-17 10:11 UTC] bwoebi@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=afd3e39d6697e36d3b92cee5da911e4ea0de3901
Log: Fixed bug #29368 (The destructor is called when an exception is thrown from the constructor).
 
PHP Copyright © 2001-2016 The PHP Group
All rights reserved.
Last updated: Sat Dec 10 18:01:38 2016 UTC