|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-03-25 12:20 UTC] eyglys at yahoo dot com dot br
Description:
------------
Destructor method is actived two times
Reproduce code:
---------------
/* source code */
<?
class t {
function __construct() {
echo "constructor\n";
}
function __destruct() {
echo "destructor\n";
}
}
$p = new t();
?>
Expected result:
----------------
/* the output expected is */
constructor
destructor
Actual result:
--------------
/* the result output */
constructor
destructor
destructor
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 21:00:01 2025 UTC |
Well in fact it seams to be a cause of IIS server, implicit call of destructor is done before the end of the script something like that : class Object { public function __construct() { ... ... echo 'end __construct'; } public function __destruct() { ... ... echo 'end __destruct'; } } echo 'Construct'; $obj = new Object(); echo 'Done'; Will result : Construct end __construct end __destruct Done I tried this script on IIS Server and Apache Server both with PHP 5.0.4 On Apache right result but not on IISAfter several minutes of testing I found out that destructor isn't invoked (after creating an instance) if the constructer has parameters. So without parameters the destructor is invoked twice(first after creating, then after destruction the last link). ------------------------------------- <?php class t { function __construct($new) { echo "constructor\n"; } function __destruct() { echo "destructor\n"; } } $p = & new t($new); ?> ------------------------------------ //Out: constructor destructor ------------------------------------- <?php class t { function __construct() { echo "constructor\n"; } function __destruct() { echo "destructor\n"; } } $p = & new t($new); ?> ------------------------------------- //Out: constructor destructor destructor ------------------------------------- So "& new t($new)" (param $new is requered) and parameters in definition of the constructor make it work right.