note 34077 deleted from language.references.spot by aidan

From: Date: Mon, 30 Aug 2004 07:43:41 +0000
Subject: note 34077 deleted from language.references.spot by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75655@lists.php.net to get a copy of this message
Note Submitter: berni at ask-us dot at ---- Some corrections: Here is the working function: It's documented because now I understand it myself (-: <? function get_var_id(&$var) { /* first of all, we need an array to store * the checked references. */ static $addr=array(); /* in order to see if $var refers to the * same data as any of the previously * checked references, we have to record * their actual values. */ foreach ($addr as $k=>$v) $addr[$k]['cmp']=$v['ref']; /* now if the value of $var is changed, * we know exactly that any recorded * reference that changed in the same way * must refer to the same data. * * copy the DATA $var refers to ! */ $sav = $var; /* replace the original DATA of $var * with "find refs to ".$var * after this operation, $var refers to * the same DATA as before ! */ $var = "find refs to ".$var; /* look for changed values. */ foreach ($addr as $k=>$v) {; /* skip arrays and objects because * of backreferences: == would fail! * BESIDES: we changed DATA to a * string, so this one cannot refer * to the same data. */ if (is_array ($v['ref'])) continue; if (is_object ($v['ref'])) continue; if ($v['ref']!==$v['cmp']) { /* this one changed! */ $return=$k; /* because we won't record the * reused reference, we don't * need to look further. * every DATA can only have 1 * reference in $addr! */ break; } } /* copy original DATA of $var back. * now all references refer to the * original data again. */ $var = $sav; /* record only new references! */ if (!isset ($return)) $addr[]=array('ref'=>&$var); /* return the id. */ return count ($addr); } ?> ... now if you take out the comments, this is just a few lines. PS: Excuses for my previous post. I got things mixed up because I'm used to Perl (with a complete different implementation of references)

« previous php.notes (#75655) next »