|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Dec 01 20:00:01 2025 UTC |
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; }