2

Is there any way to check for identity (i.e. being exactly the same object, occupying one and only one place in memory) for two variables representing arrays or objects? (i.e. when one modifies the object named by one variable the changes can be seen in the value of the other variable as they point to the same object/array)

The === operator, for example, checks if two arrays are "identical" in the sense that their elements and their ordering are equal (as opposed to == that doesn't check ordering for arrays, so for $a = [11, 22]; $b = [1 => 22; 0 => 11];, $a == $b is true but $a === $b is false (because in this latter case the ordering differs, arrays being ordered maps).

My imagined are_identical function would work like this (somewhat like the is in Python):

$a = [11, 22];
$b = [11, 22];
are_identical($a, $b); # => false

$x = [11, 22];
$y = &$x;
are_identical($x, $y); # => true
NeuronQ
  • 7,527
  • 9
  • 42
  • 60
  • 1
    Probably this will help you.. http://stackoverflow.com/questions/5153528/how-check-memory-location-of-variable-in-php – Artem L Jan 09 '13 at 13:48
  • @ArtemL thanks, I'm just taking a look at it... – NeuronQ Jan 09 '13 at 13:59
  • I would like to mention that there is usually a solution for your problem that does not require you to know whether two variables point to the same array. Especially since it is very easy to end up with a new array in PHP. – dualed Jan 09 '13 at 14:00
  • Dup/similar questions: http://stackoverflow.com/questions/4817562/detecting-whether-a-php-variable-is-a-reference-referenced and http://stackoverflow.com/questions/1541865/php-how-to-know-if-a-variable-is-a-reference and also http://stackoverflow.com/questions/3148125/php-check-if-object-array-is-a-reference – SDC Jan 09 '13 at 14:05

3 Answers3

3

You can't do this with arrays in PHP; however, you can change your code to use ArrayObject and make your comparisons using the === operator.

In PHP, the === operator will only return true if the objects are the same instance of the same class (i.e. they refer to the same object in memory).

Colin M
  • 13,010
  • 3
  • 38
  • 58
1

Objects in PHP can be tested to see if they are the same instance by using triple-equal:

if($obj1 === $obj2) {....}

However, arrays are not objects in PHP, so the triple-equal trick doesn't work for them in the way it does for objects.

That said, this php manual page may help as well: Spotting References. There are a couple of functions in the comments which claim to be able to detect variables that are references to each other.

SDC
  • 14,192
  • 2
  • 35
  • 48
0

OK, thanks Artem L and SDC: yes, others have asked about this but in different form so I didn't know what to look for.

This snippet of code seems to fill the purpose of my are_identical function (negated):

function is_reference_to(&$a, &$b)
{
    if ($a !== $b) return false;

    $temp = $a;
    $checkval = $a === null ? "" : null;
    $a = $checkval;

    if ($b === $checkval) {
        $a = $temp;
        return true;
    } else {
        $a = $temp;
        return false;
    }        
}
NeuronQ
  • 7,527
  • 9
  • 42
  • 60