Re: PHP 4.0 Bug #7659: Class variables get confused

From: Date: Mon, 06 Nov 2000 11:38:26 +0000
Subject: Re: PHP 4.0 Bug #7659: Class variables get confused
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-37251@lists.php.net to get a copy of this message
awilkins@hmc.edu writes: > From: awilkins@hmc.edu > Operating system: RedHat Linux 6.2 > PHP version: 4.0 Latest CVS (06/11/2000) > PHP Bug Type: Scripting Engine problem > Bug description: Class variables get confused > > When this script is run: > > <?php > class Three { > var $one, $two; > > function Three($a, $b) { > echo "a is $a; b is $b<br>\n"; > > $this->$one = $a; ^^^^ > echo "one is " . $this->$one . "; two is " . $this->$two . > "<br>\n"; ^^^^ ^^^^ Those dollar signs shouldn't be there; the syntax you want for this is '$this->one = $a', etc. What's happening above is that $this->$varname is an allowed syntax, but with a different meaning. When PHP sees $this->$varname, it evaluates $varname to find out what the name of the desired member is, then evaluates the rest of it. So, for instance, if $varname == 'one', then $this->$varname = 'Bob' would be interpreted as $this->one = 'Bob'. Since you haven't assigned anything to $one or $two, it's interpreting the name to be NULL (a weird but possible thing in PHP). Insert the following lines at the end of Three() to see what I mean: $null = NULL; echo "NULL is " . $this->$null . "<br>\n"; Hope this helps, Torben > a is 1; b is 2 > one is 1; two is 1 > one is 2; two is 2 > > For some reason, it appears as if the two separate variables $one > and $two are pointing at the same thing. This doesn't appear to be > a problem that is specific to the constructor, as it happens even if > the function involved is only a normal function. It also occurs if > the variables are modified outside of the class. > > I also found this bug in 4.0.3pl1, but have not tried any earlier > version. The only module I compiled PHP with was pgsql, if it > matters. > > Alex Wilkins -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.dev (#37251) next »