berni at ask-us dot at
16-Jul-2003 12:07
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)