note 34077 added to language.references.spot
| From: | berni at ask-us dot at | Date: | Tue, 15 Jul 2003 17:07:57 +0000 |
| Subject: | note 34077 added to language.references.spot | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-52144@lists.php.net to get a copy of this message | ||
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)
----
Manual Page -- http://www.php.net/manual/en/language.references.spot.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34077
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34077&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34077&report=yes