Re: Class var not retaining values

From: Date: Mon, 21 May 2001 23:19:09 +0000
Subject: Re: Class var not retaining values
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-53792@lists.php.net to get a copy of this message
Could it be that you need your for loop to iterate <= instead of < ? As it is, I don't think it will ever show anything. After all, you are setting $index to 0 at the beginning of the function every time you call it. My guess is you actually want to keep track of $index as an instance variable outside the function and reference it via $this->index in all the places where you have $index. class AR { var $index = 0; function AddReason($score, $reason, $id) { $this->reasons[$this->index] = "$score|$reason|$id"; for ($i = 0; $i <= $this->index; $i++) { $out = $this->reasons[$i]; echo "$out..."; } $this->index++; return $score; } } Pete. ""Bob"" <bstremel@optonline.net> wrote in message news:9ec2rm$etg$1@toye.p.sourceforge.net... > I have a class defined with a var $reasons that I will use as an array. The > code to add to it is: > > function AddReason($score, $reason, $id) > { > static $index = 0; > $this->reasons[$index] = "$score|$reason|$id"; > for ($i = 0; $i < $index; $i++) > { > $out = $this->reasons[$i]; > echo "$out..."; > } > $index++; > return $score; > } > > However, every time I call it, the value stored in the reasons array seems > to disappear, and the array holds no data. It seems to be behaving like a > local variable, even though it is in a class. Any suggestions would be > greatly appreciated. > > Thanks

« previous php.general (#53792) next »