|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-10-23 10:43 UTC] joustin at post dot pl
Description:
------------
This is quite interesting. Appeared after upgrading to 5.2.4 and broke my code.
It's great that objects are finally destroyed in the right order, but a whole lot of code still relies on "destroy all" or "cleanup" functions.
What struck me is that as of 5.2.4, my object in the moment of calling it's destructor, in global scope, appears as NULL.
I've also devised a simple and dirty workaround, which is found in the code below.
Thanks!
Reproduce code:
---------------
class foo {
function __construct(){
echo "Constructed foo\n";
}
function getDb(){
echo "Getting DB";
}
function __destruct(){
echo "Destructing foo\n";
echo "Trying to destruct bar\n";
global $bar;
if(false){ // DIRTY WORKAROUND
global $foo;
$foo = $this;
}
$bar->__destruct();
}
}
class bar {
protected $destroyed = false;
function __construct(){
echo "Constructed bar\n";
}
function __destruct(){
if(!$this->destroyed){
echo " Destructing bar\n";
global $foo;
echo " Our global parent, foo, is of type ".gettype($foo)."\n";
$this->destroyed = true;
}
}
}
$bar = new bar();
$foo = new foo();
echo "--\nScript finishes...\n";
Expected result:
----------------
Constructed bar
Constructed foo
--
Script finishes...
Destructing foo
Trying to destruct bar
Destructing bar
Our global parent, foo, is of type object
Actual result:
--------------
Constructed bar
Constructed foo
--
Script finishes...
Destructing foo
Trying to destruct bar
Destructing bar
Our global parent, foo, is of type NULL
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 16:00:01 2025 UTC |
The foo::__destruct() is called during destruction of global variable $foo. The folowing script is a simplified example and its behavior was never changed. <?php class foo { function __destruct() { global $foo; var_dump($foo); // prints NULL, because $foo is alredy destoied } } $foo = new foo(); unset($foo); ?> php-5.2.4 just makes unset() for objects automaticly (in right order).