php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6962 Unsetting a global object in a function doesn't affect other functions
Submitted: 2000-09-30 15:57 UTC Modified: 2000-10-01 06:57 UTC
From: rubein at earthlink dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.1pl2 OS: Linux 2.2.16
Private report: No CVE-ID: None
 [2000-09-30 15:57 UTC] rubein at earthlink dot net
script:


<?

class TEST {
        var $foo;
}

function one()
{
        global $object;

        $object = new TEST();

        $object->foo = "bar";

        echo "\none() dump of object before unset\n";
        var_dump($object);

        unset($object);

        echo "\none() dump of object after unset\n";
        var_dump($object);

        two();
}

function two()
{
        global $object;
        echo "\ntwo() dump of object after unset\n";
        var_dump($object);
}

echo "<PRE>\n";
one();

echo "\n(no function) dump of object after unset\n";
var_dump($object);



produces:
one() dump of object before unset
object(test)(1) {
  ["foo"]=>
  string(3) "bar"
}

one() dump of object after unset
NULL

two() dump of object after unset
object(test)(1) {
  ["foo"]=>
  string(3) "bar"
}

(no function) dump of object after unset
object(test)(1) {
  ["foo"]=>
  string(3) "bar"
}



unset()ing an globally defined object within a function doesn't unset it globally - it can still be referred to in other functions. 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-10-01 06:21 UTC] rubein at earthlink dot net
Workaround:

Change:

unset($object);

to:

unset($object);
unset($GLOBALS["object"]);

It seems that while changes to the object affect the global instance of it, unsetting it simply removes the global alias from the script. While my suggested workaround works, it would definitely appear to be a bug.

I'm poking into the source and taking a look at this one, but I don't understand the PHP source that well. I'm curious now though.
 [2000-10-01 06:57 UTC] waldschrott@php.net
this is intended behaviour and no bug, if you really want to remove it from the global scope, use your workaround
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jun 16 21:01:29 2024 UTC