Re: Re: CLASS/Object lifetime - > more info
| From: | Roger Helgesen | Date: | Wed, 22 Feb 2006 07:38:26 +0000 |
| Subject: | Re: Re: CLASS/Object lifetime - > more info | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-230885@lists.php.net to get a copy of this message | ||
ain't that the other way around ?
$var1=$var2 ($var1 is a copy of $var2)
$var1=& $var2 ($var1 is a reference to $var2)
$var1='Test'
echo $var2 (outputs Test)
roger
Jochem Maas wrote:
REFERENCES. the object you get back is a copy (foreach has the same effect as creating a new variable as in the example below). ... see below ... notice the use of the & symbol (and the 'refcount' values). (there is loads of info out there on references/php4/objects out there that explain it better than me.) - see what happens when you start removing '&'s. or you could try php5 and forget about 'object hell'. :-) roger helgesen wrote:-- mvh Roger Helgesen Helgesen Tekniske-bygg AS 56193410 roger@ht-gruppen.noThe function that gives me trouble --------- function fyll_sub_konto($pv){class Test { var $number; var $subposter; function Test($n = 0) { $this->number = $n; } function sqar() { if (is_numeric($this->number)) { $this->number *= $this->number; } return $this; } function doit() { echo strval($this->number),"\n"; } function make() {foreach($this->subposter AS $konto){ $konto->fyll_fra_post($pv); }static $c = 1; $t = new Test($c++); $s =& $t; // watch what happens when you remove the '&' (php4 only) $s->sqar(); $this->subposter[] = $t->sqar();} function goan() {$cnt = count($this->subposter); $this->doit(); for($i=0;$i<$cnt;$i++) { // watch what happens when you remove the '&' (php4 only) $tmp =& $this->subposter[$i]; $tmp->sqar(); $this->subposter[$i]->doit(); }} } $t = new Test("infinity"); $i = 0; while($i++ < 5) $t->make(); $t->goan();} ------------- $this->subposter is the array of CLASS2 function fyll_fra_post($post_vars){echo "(gruppe)class = ".get_class($this) ."<br>\n"; $teller=0; foreach($this->poster AS $post=>$verd){ $best="bestilt_".$post; $avik="avvik_".$post; $fakt="faktura_".$post; $this->poster[$post]["bestilt"]=$post_vars[$best]; $this->poster[$post]["avvik"]=$post_vars[$avik]; $this->poster[$post]["faktura"]=$post_vars[$fakt]; } $this->endret='ja'; $this->sett_sum();} If I print the data (after setting the new data) inside 'fyll_fra_post' it puts out the new data. If I do the same from 'fyll_sub_konto' (after foreach) it gives me the old data.