1

I'd like to be able to inquire my program as to what variable a variable is a reference of. Is there a way to do this? I looked in all the usual sources and couldn't find anything, perhaps my search terms were goofy. Thanks.

linus72982
  • 1,418
  • 2
  • 16
  • 31
  • 1
    will be more helpful to answer ur ques if you u give an example – PHP Apr 09 '11 at 05:57
  • do you want to know if two variables are references to the same thing? e.g., `$a = &$b;` means that `is_ref($a, $b) === true`? If so, see my answer. If not, then I've misunderstood. – Matthew Apr 09 '11 at 06:16
  • http://stackoverflow.com/questions/4817562/detecting-whether-a-php-variable-is-a-reference-referenced/4817962#4817962 Not exactly what you want, but may help. – NikiC Apr 09 '11 at 08:13
  • It's all much simpler than I think people are guessing. I have two arrays in an object that contain the same combined information but ordered and organized in completely different ways to ease some further functions down the line. I create the second array as references to the variables in the first - I was just wondering if I could reverse the process and check what variable a variable is a reference to - see what I mean? – linus72982 Apr 10 '11 at 00:26

4 Answers4

3

you can just compare references with === operator

Note: this only compares object references.

$obj1 = new DateTime();
$obj2 = $obj1;

if($obj2 === $obj1){
    echo "Equal";
}else {
    echo "Not Equal";
}
// Outputs Equal


$obj2 = new DateTime();

if($obj2 === $obj1){
   echo "Equal";
}else {
   echo "Not Equal";
}
// Outputs Not Equal
Headshota
  • 21,021
  • 11
  • 61
  • 82
  • === is "completely identical" and does not know about, nor care about references. It will be true if the things being compared have the same value, even if they are actually different variable that are entirely unconnected. It can not help you find a reference. – Charles Apr 09 '11 at 06:17
  • @Charles in object context === operator compares if variables point to the same instance of the same class, thus it can be used. but checking if the variables are of the needed class instance is another case. – Headshota Apr 09 '11 at 06:20
  • That only means it works for objects, though, not arrays, strings, floats, bools, etc. Further, because objects work by implicit reference, it can't tell the difference between `$bar = $foo` and `$baz = &$foo`... not that there is any. – Charles Apr 09 '11 at 06:35
  • 1
    yes that's correct, I just thought he needed to check object references. I'll edit my post to inform that. thanks. – Headshota Apr 09 '11 at 06:42
  • Indeed, he has yet to tell us exactly what he needs this functionality for. – Charles Apr 09 '11 at 06:52
0

The only code I personally know is to directly query what a class is of, not to retrieve it:

if($var instanceof Your_Class) { }

If you know the variable is strictly a class, you can use:

get_class();

If you use the function on a non-object, it appears to return an E_WARNING. I'd suggest code such as this:

$class_known = false;
if(is_object($class))
{
    $class_name = get_class($class);
    $class_known = false;
}

if($class_known)
{
    echo $class_name;
}
Jeremy Dentel
  • 837
  • 2
  • 9
  • 19
0

If you're dealing with objects, I believe === performs the test you want.

$obj1 = new Object;
$obj2 = $obj1;

echo ($obj1 === $obj2) ? 'same' : 'not same'; // same

$obj2 = new Object;

echo ($obj1 === $obj2) ? 'same' : 'not same'; // not same
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
0

As far as I know, there's no direct way to test if two variables point to the same thing. Something like this might work:

function is_reference_of(&$a, &$b)
{
  // if an object, use strict comparison
  if (is_object($a)) return $a === $b;

  // definitely not a reference to each other if they aren't the same value
  if ($a !== $b) return false;

  $tmp = $a;

  if (is_array($a))
  {
    $a = 0;
    // if $b is no longer an array, then it must be a reference to $a
    $is_ref = !is_array($b);
  }
  else
  {
    $a = !$a;
    // if $b is no longer equal to $tmp, then it must be a reference to $a
    $is_ref = ($b !== $tmp);
  }

  // make sure to reset $a back to what it was
  $a = $tmp;

  return $is_ref;
}

$a = 0;
$b = 0;

is_reference_of($a, $b); // false

$b = &$a;

is_reference_of($a, $b); // true

Huge disclaimer: I haven't really thought this through carefully. There could be all sorts of side effects to doing something like the above. Consider this just a proof of concept to get you started

If you are always working with arrays or classes, you could try setting a temporary field or property in one and seeing if it exists in the other, etc. That would be less prone to errors than the above code that changes the entire variable.

Note that the order of the parameters in the above function does not matter.

Update: With objects, you can use === and skip the function altogether. I've updated the function slightly to detect what type of variable is being tested and react accordingly. The disclaimer still applies.

Matthew
  • 47,584
  • 11
  • 86
  • 98