php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36837 construct, call another classes fn, throw,catch,die. destructor is called
Submitted: 2006-03-23 20:47 UTC Modified: 2006-03-24 01:49 UTC
From: tz at universale dot hu Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.1.2 OS: Windows XP
Private report: No CVE-ID: None
 [2006-03-23 20:47 UTC] tz at universale dot hu
Description:
------------
Normally, if the constructor throws an exception, the destructor is not called reaching the end of the script.
That's OK

In the code attached, the destructor is called.

There are two important parts of the code:
1. The exception must be generated by an object called from the foo's constructor. 
2. It has to handle this exception, and call 'die'

In this case, the foo-s destructor is called.


Reproduce code:
---------------
class foo {
    function __construct(){
        new thrower( this );
    }
    
    function __destruct(){
        sleep( 5 );        
    }
}
class thrower {
    function __construct(){
        try {
            throw new Exception( "foomessage" );
        } catch ( Exception $e ){
            die;
        }
    }
}

$f = new foo();


Expected result:
----------------
the destructor mustn't be called


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-23 21:00 UTC] bjori@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 destructor is called during the script shutdown
 [2006-03-24 01:49 UTC] tz at universale dot hu
I double-checked the documentation and found nothing related to this problem.
In a similar case, in the code i submit now, the destructor is _not_ called. 

So, what is the difference?

Yours sincerely
tz


class foo {
    function __construct(){
       throw new Exception( "foomessage" );
    }
    
    function __destruct(){
        sleep( 5 );        
    }
}

try {
    $f = new foo();
} catch ( Exception $e ){
    die;
}
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 14:01:34 2025 UTC