php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #43080 Global object is null before destroying it.
Submitted: 2007-10-23 10:43 UTC Modified: 2007-11-01 11:43 UTC
From: joustin at post dot pl Assigned: dmitry (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.4 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: joustin at post dot pl
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-25 12:55 UTC] jani@php.net
Upgraded from what PHP version..? AFAIK, this is how it should work, so I don't understand why you report a bug about it..
 [2007-10-25 13:06 UTC] joustin at post dot pl
Upgraded from 5.2.3 to 5.2.4. 

I could not find any reference to if (and why) it would change. Changelog gives no clue either.

It happens to globally defined vars - php engine nulls the reference to object before calling it's destructor. IMO illogical and inconsistent. Worked fine before 5.2.4.
 [2007-10-25 13:28 UTC] jani@php.net
Dmitry, can you check this out please.
 [2007-11-01 11:43 UTC] dmitry@php.net
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).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 15:01:32 2024 UTC