note 103383 added to language.oop5.decon
| From: | boukeversteeg at gmail dot com | Date: | Mon, 11 Apr 2011 14:12:30 +0000 |
| Subject: | note 103383 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178685@lists.php.net to get a copy of this message | ||
Php 5.3.3 had a very strange bug, related to __destruct, but this bug was fixed in 5.3.6. This bug
happened to me and it took me forever to figure it out, so I wanted to share it.
In short, don't unset properties in __destruct():
<?php
class Foobar {
public $baz;
function __destruct() {
# Don't do either of these, if $baz also has a __destruct()!
$this->baz = null;
unset($this->baz);
# Instead, don't clear it at all, or do this:
$this->baz->__destruct();
}
}
?>
If you made this mistake, this might happen in php<5.3.6:
<?php
# Some function that throws an exception
function fail($foobar) {
throw new Exception("Exception A!");
}
$foobar = new Foobar();
$foobar->baz = new Foobar();
try {
fail($foobar); // Send foobar to func that throws an Exception
} catch( Exception $e ) {
print $e->getMessage(); // Exception A will be caught and printed, as expected.
}
$foobar = null; // clearing foobar, and its property $baz
try {
print 'Exception B:';// this will be printed
// output stops here.
throw new Exception("Exception B!");
} catch( Exception $e ) {
print $e->getMessage(); // doesn't happen
}
print 'End'; // this won't be printed
?>
----
Server IP: 69.147.83.197
Probable Submitter: 192.87.139.139
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/103383
Del: integrated -- https://master.php.net/note/delete/103383/integrated
Del: useless -- https://master.php.net/note/delete/103383/useless
Del: bad code -- https://master.php.net/note/delete/103383/bad+code
Del: spam -- https://master.php.net/note/delete/103383/spam
Del: non-english -- https://master.php.net/note/delete/103383/non-english
Del: in docs -- https://master.php.net/note/delete/103383/in+docs
Del: other reasons-- https://master.php.net/note/delete/103383
Reject -- https://master.php.net/note/reject/103383
Search -- https://master.php.net/manage/user-notes.php