note 34072 deleted from language.references.spot by sniper

From: Date: Tue, 30 Sep 2003 01:26:32 +0000
Subject: note 34072 deleted from language.references.spot by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57354@lists.php.net to get a copy of this message
Note Submitter: berni@ask-us.at ---- This chapter is called "Referenzen entdecken" in german! I don't know if it wants to say "find variables that are references within your script" or just "find out about what references are"... Anyway, I read about a feature request for a function get_all_references(&$x); What should be the output ? An array of references ? You cannot tell a difference between 2 refs to the same data, can you ? The only thing that is missing is not a function! It's the operator ==& meaning ($x==&$y) ? '$x refers to the same data as $y' : 'they do not refer to the same data' I wrote an experimental function to do a similar thing: <? function get_var_id(&$var) { static $addr=array(); $addr[] = array ('ref' => &$var); // re-"copy" all recorded references: foreach ($addr as $k=>$v) { // find a "SCALAR" value to compare: if (is_array ($v['ref']) && isset ($v['ref'][0])) { if (!is_array ($v['ref'][0]) && !is_object ($v['ref'][0])) $addr[$k]['cmp']=$v['ref'][0]; else unset ($addr[$k]['cmp']); } elseif (is_object ($v['ref']) && isset ($v['ref']->a)) { if (!is_array ($v['ref']->a) && !is_object ($v['ref']->a)) $addr[$k]['cmp']=$v['ref']->a; else unset ($addr[$k]['cmp']); } elseif (!is_array ($v['ref']) && !is_object ($v['ref'])) { $addr[$k]['cmp']=$v['ref']; } else { unset ($addr[$k]['cmp']); } } // look for $var in $addr: if (is_array ($var)) $var[0] = "find refs to ".$var[0]; elseif (is_object ($var)) $var->a = "find refs to ".$var->a; else $var = "find refs to ".$var; $return = -1; foreach ($addr as $k=>$v) { if (is_array ($v['ref'])) { if (isset ($v['ref'][0])!=isset ($v['cmp']) || $v['cmp'].""!="".$v['ref'][0]) { $return = $k; break; } } elseif (is_object ($v['ref'])) { if (isset ($v['ref']->a)!=isset ($v['cmp']) || $v['cmp'].""!="".$v['ref']->a) { $return = $k; break; } } else { if ($v['cmp'].""!="".$v['ref']) { $return = $k; break; } } } // restore if (is_array ($var)) { if (isset ($v['cmp'])) $var[0]=$v['cmp']; else unset ($var[0]); } elseif (is_object ($var)) { if (isset ($v['cmp'])) $var->a=$v['cmp']; else unset ($var->a); } else { $var = $v['cmp']; } // keep new addr ? if (++$return < count ($addr)) array_pop ($addr); return $return; } $x=array(1,2,&$x); $y=&$x; $c="ivo"; $d="berni"; echo "address of \$x is ".get_var_id (&$x)."<br>"; echo "address of \$x is ".get_var_id (&$x)."<br>"; echo "address of \$x is ".get_var_id (&$x)."<br>"; echo "address of \$y is ".get_var_id (&$y)."<br>"; echo "address of \$c is ".get_var_id (&$c)."<br>"; echo "address of \$d is ".get_var_id (&$d)."<br>"; ?> How does it work now ? echo (get_var_id ($x) == get_var_id ($y)) ? '$x==&$y' : '$x!=&$y'; Of course this function has nothing to loose in a productive environment. I will try to put it into a php extension (never did this and I don't know if it is possible) But maybe you can use it for debugging...

« previous php.notes (#57354) next »