Re: Small riddle with classes and references, or bug?

From: Date: Mon, 22 Dec 2003 11:33:41 +0000
Subject: Re: Small riddle with classes and references, or bug?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-173142@lists.php.net to get a copy of this message
On Mon, Dec 22, 2003 at 11:57:58AM +0100, rush wrote: > Hello, > > I have a problem understanding following chunk of code. It seems that global > $rme does not remember an reference to $this, but instead it is remembering > null or something like that: > > <?PHP > > class A { > function rememberMe() { > global $rme; this creates a reference from $rme to $GLOBALS['rme'] > $rme = & $this; this create a *new* reference from $rme to $this. so only a local $rme is referencing $this now. change the line to $GLOBALS['rme'] = & $this; to get the expected behaviour. > $this->someInstanceVar =77; > } > > function report() { > global $rme; > echo 'X'; print_r($rme); echo 'X'; > } > } > > $a = new A(); > $a->rememberMe(); > $a->report(); > > ?> > > This little script outputs: > XX > > While I would expect it to return: > > Xa Object ( [someInstanceVar] => 77 ) X > > rush

« previous php.general (#173142) next »