PHP 4.0 Bug #8937 Updated: unset() in context with GLOBAL

From: Date: Thu, 08 Mar 2001 16:23:25 +0000
Subject: PHP 4.0 Bug #8937 Updated: unset() in context with GLOBAL
Groups: php.dev 
Request: Send a blank email to php-dev+get-48278@lists.php.net to get a copy of this message
ID: 8937 User Update by: aulbach@unter.franken.de Old-Status: Feedback Status: Open Bug Type: Scripting Engine problem Description: unset() in context with GLOBAL My problem is, that old PHP3 scripts won´t work with PHP4, if they use such a construct. And it is a bug in the meaning of "the program dosn't do, what I expect". Ok, the bug is documented, but look at the ERROR-ID #8972 for a case, that makes a much more surprising and unexpected effect. Why can't PHP just overwrite the value with the value "UNSET" in the case of unsetting a reference - instead of removing the reference? Previous Comments: --------------------------------------------------------------------------- [2001-03-06 08:10:47] stas@php.net So, what's the problem with your latest code? I do not see any. --------------------------------------------------------------------------- [2001-01-28 18:53:34] aulbach@unter.franken.de Bug or feature. Thats the question. :) Sorry, it IS a bug. 4 people have mentioned this and I found now a test case, where no workarround exists. But I found another problem in this context. Will make a new ticket. --------------------------------------------------------------------------- [2001-01-26 16:24:50] derick@php.net Sorry... forgot to read all the comments.... --------------------------------------------------------------------------- [2001-01-26 16:14:57] derick@php.net This isn't a bug, but a feature =) If you use GLOBAL within a function it creates a reference to the orignal variabele, this reference is local to the function. SO if you unset() this variabele, you're only unsetting the reference (and thus not the GLOBAL variabele). This is documented behavior (from the unset() manual page): If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called. --------------------------------------------------------------------------- [2001-01-26 14:19:15] aulbach@unter.franken.de The mentioned workarround (unset($GLOBALS['x'])) dosn't work with my testscript. The result is exactly the same as with the first version of my script. I really have GREAT TROUBLES to upgrade this old application to PHP4. Result: '/hugo/bla/fasel' '/hugo/bla' '/hugo' DIR called with function test1(): Array ( [/hugo] => 1 [/hugo/bla] => 1 [/hugo/bla/fasel] => 1 ) '/hugo/bla/fasel' '/hugo/bla' '/hugo' DIR called with function test2(): Array ( [/hugo/bla/fasel/test] => 1 [/hugo] => 1 [/hugo/bla] => 1 [/hugo/bla/fasel] => 1 ) Script: <? function test1 ($dir) { GLOBAL $x; unset($GLOBALS["x"]); $x[$dir]=true; mydirname($dir); } function test2 ($dir) { GLOBAL $x; ####### unset($x); no unset! $x[$dir]=true; mydirname($dir); } function mydirname ($dir) { GLOBAL $x; $dir=ereg_Replace('/[^/]*$','',$dir); if (!empty($dir)) { echo "'$dir'<br>"; flush(); mydirname($dir); $x[$dir]=true; } } $dir="/hugo/bla/fasel/test"; # first char must be '/' ! unset($x); test1($dir); echo "DIR called with function test1(): "; print_r($x); echo "<br>"; unset($x); test2($dir); echo "DIR called with function test2(): "; print_r($x); ?> --------------------------------------------------------------------------- The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online. Full Bug description available at: http://bugs.php.net/?id=8937

« previous php.dev (#48278) next »