|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2002-04-20 10:31 UTC] marcus at vulcanus dot its dot tudelft dot nl
While using classes, pointers and globals I noticed data not being migrated to the globalsphere due to pointerassignments.
[Example]
class test {
var $v=0;
function set($i) { $this->v = $i; }
}
$a = new test();
$b =& new test();
$c =& $b;
function ta() {
global $a,$b,$c;
$a->set(1);
$b =& $a;
$c->set(2);
}
ta();
echo "$a->v,$b->v,$c->v";
[/example]
This should display 1,1,2 since $b has been set to $a, but this pointer-assigment isn't emigrated to the global $b.
I have done many expirements with it to see if i could further specify the bug... at first i thought it was destroying non-globals that got pointed too at the end of the functioncall and thus (accidently) destroying the pointers in the process, but then if i would point $a to $b and export them both, neither should be destroyed, so the reference should stay intact... it isn't.
So the only thing that remains is that function-local pointer-assigments not seem to affect my global var.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 11:00:01 2025 UTC |
The assignment does work while staying local btw... so an echo $b->v; within ta() would display 1 as it should. I'm not sure about the exact implementation of PHP, but i think globals ARE pointers, right ? That would explain it (though i'm not happy with it). If so the line global $b; would do the same as $b(local) =& $b(global); and thus a reassignment of $b to another point would seperate him from $b(global). Still i would like to re-reference my $b(global) in a local function. Is there maybe a workaround or is it an impossibility ?