|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2009-02-16 18:48 UTC] niximor at gmail dot com
 Description:
------------
When I use extract() with array, that contains key "this", it behaves really weird. See code for more info.
Reproduce code:
---------------
class Test1 {
	var $name = "test1";
}
class Test2 {
	var $name = "test2";
	function bug() {
		extract(array("this"=>new Test1()), EXTR_OVERWRITE | EXTR_REFS);
		echo get_class($this)."::name = ".$this->name;
	}
}
$t = new Test2();
$t->bug();
Expected result:
----------------
Test1::name = test1 (preferably)
OR
Test2::name = test2
- not combination of both.
Actual result:
--------------
Test1::name = test2
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 01:00:01 2025 UTC | 
I think your second proposal on expected behavior would be more correct, rewriting $this doesn't seem right :p Below is a simple patch: Index: ext/standard/array.c =================================================================== --- ext/standard/array.c (revision 289616) +++ ext/standard/array.c (working copy) @@ -1364,6 +1364,10 @@ if (var_exists && var_name_len == sizeof("GLOBALS") && !strcmp(var_name, "GLOBALS")) { break; } + /* THIS protection */ + if (var_exists && var_name_len == (sizeof("this")-1) && !strcmp(var_name, "this") && EG(scope) && "" != EG(scope)->name) { + break; + } ZVAL_STRINGL(&final_name, var_name, var_name_len, 1); break;