php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16712 pointers and globals don't work together
Submitted: 2002-04-20 10:31 UTC Modified: 2002-04-20 12:46 UTC
From: marcus at vulcanus dot its dot tudelft dot nl Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.1.2 OS: Linux RedHat 7.2
Private report: No CVE-ID: None
 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-20 10:43 UTC] marcus at vulcanus dot its dot tudelft dot nl
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 ?
 [2002-04-20 12:46 UTC] mfischer@php.net
First, there are NO pointers in PHP, these are called references, please stick to this term.

Second, yes, variables in a function imported from the global namespace are in fact references to the global variables. So if working with references on references in this context does not work.

For objects, you should be out of trouble in PHP5/ZE2 as they aren't copied anymore so you don't need a reference to them.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 27 19:01:29 2024 UTC