public static function is_ref(&$var1, &$var2) {
      //If a reference exists, the type IS the same
      if(!self::same_type($var1, $var2)) {
          return false;
      }

      $same = false;

      if(is_array($var1)) {
       do {
           $key = uniqid("is_ref_", true);
       } while(array_key_exists($key, $var1));

       //The two variables differ in content ... They can't be the same
       if(array_key_exists($key, $var2)) {
         return false;
       }

       //The arrays point to the same data if changes are reflected in $var2
       $data = uniqid("is_ref_data_", true);
       $var1[$key] =& $data;
       //There seems to be a modification ...
       if(array_key_exists($key, $var2)) {
          if($var2[$key] === $data) {
               $same = true;
          }
       }

        //Undo our changes ...
        unset($var1[$key]);
      } elseif(is_object($var1)) {
        //The same objects are required to have equal class names ;-)
        if(get_class($var1) !== get_class($var2)) {
           return false;
        }

        $obj1 = array_keys(get_object_vars($var1));
        $obj2 = array_keys(get_object_vars($var2));

        //Look for an unused index in $var1
        do {
         $key = uniqid("is_ref_", true);
        } while(in_array($key, $obj1));

        //The two variables differ in content ... They can't be the same
        if(in_array($key, $obj2)) {
          return false;
        }

        //The arrays point to the same data if changes are reflected in $var2
        $data = uniqid("is_ref_data_", true);
        $var1->$key =& $data;
        //There seems to be a modification ...
        if(isset($var2->$key)) {
          if($var2->$key === $data) {
             $same = true;
          }
        }

          //Undo our changes ...
          unset($var1->$key);
        } elseif (is_resource($var1)) {
          if(get_resource_type($var1) !== get_resource_type($var2)) {
             return false;
          }

          return ((string) $var1) === ((string) $var2);
        } else {
          //Simple variables ...
          if($var1!==$var2) {
            //Data mismatch ... They can't be the same ...
            return false;
          }

          //To check for a reference of a variable with simple type
          //simply store its old value and check against modifications of the second variable ;-)

          do {
            $key = uniqid("is_ref_", true);
          } while($key === $var1);

          $tmp = $var1; //WE NEED A COPY HERE!!!
          $var1 = $key; //Set var1 to the value of $key (copy)
          $same = $var1 === $var2; //Check if $var2 was modified too ...
          $var1 = $tmp; //Undo our changes ...
        }

          return $same;
        }
public static function is_ref(&$var1, &$var2) {
      //If a reference exists, the type IS the same
      if(!self::same_type($var1, $var2)) {
          return false;
      }

      $same = false;

      if(is_array($var1)) {
       do {
           $key = uniqid("is_ref_", true);
       } while(array_key_exists($key, $var1));

       //The two variables differ in content ... They can't be the same
       if(array_key_exists($key, $var2)) {
         return false;
       }

       //The arrays point to the same data if changes are reflected in $var2
       $data = uniqid("is_ref_data_", true);
       $var1[$key] =& $data;
       //There seems to be a modification ...
       if(array_key_exists($key, $var2)) {
          if($var2[$key] === $data) {
               $same = true;
          }
       }

        //Undo our changes ...
        unset($var1[$key]);
      } elseif(is_object($var1)) {
        //The same objects are required to have equal class names ;-)
        if(get_class($var1) !== get_class($var2)) {
           return false;
        }

        $obj1 = array_keys(get_object_vars($var1));
        $obj2 = array_keys(get_object_vars($var2));

        //Look for an unused index in $var1
        do {
         $key = uniqid("is_ref_", true);
        } while(in_array($key, $obj1));

        //The two variables differ in content ... They can't be the same
        if(in_array($key, $obj2)) {
          return false;
        }

        //The arrays point to the same data if changes are reflected in $var2
        $data = uniqid("is_ref_data_", true);
        $var1->$key =& $data;
        //There seems to be a modification ...
        if(isset($var2->$key)) {
          if($var2->$key === $data) {
             $same = true;
          }
        }

          //Undo our changes ...
          unset($var1->$key);
        } elseif (is_resource($var1)) {
          if(get_resource_type($var1) !== get_resource_type($var2)) {
             return false;
          }

          return ((string) $var1) === ((string) $var2);
        } else {
          //Simple variables ...
          if($var1!==$var2) {
            //Data mismatch ... They can't be the same ...
            return false;
          }

          //To check for a reference of a variable with simple type
          //simply store its old value and check against modifications of the second variable ;-)

          do {
            $key = uniqid("is_ref_", true);
          } while($key === $var1);

          $tmp = $var1; //WE NEED A COPY HERE!!!
          $var1 = $key; //Set var1 to the value of $key (copy)
          $same = $var1 === $var2; //Check if $var2 was modified too ...
          $var1 = $tmp; //Undo our changes ...
        }

          return $same;
        }
public static function is_ref ( & $ var1 , & $ var2 ) { //If a reference exists, the type IS the same if ( ! self :: same_type ( $ var1 , $ var2 ) ) { return false ; } $ same = false ; if ( is_array ( $ var1 ) ) { do { $ key = uniqid ( "is_ref_" , true ) ; } while ( array_key_exists ( $ key , $ var1 ) ) ; //The two variables differ in content ... They can't be the same if ( array_key_exists ( $ key , $ var2 ) ) { return false ; } //The arrays point to the same data if changes are reflected in $var2 $ data = uniqid ( "is_ref_data_" , true ) ; $ var1 [ $ key ] = & $ data ; //There seems to be a modification ... if ( array_key_exists ( $ key , $ var2 ) ) { if ( $ var2 [ $ key ] === $ data ) { $ same = true ; } } //Undo our changes ... unset ( $ var1 [ $ key ] ) ; } elseif ( is_object ( $ var1 ) ) { //The same objects are required to have equal class names ;-) if ( get_class ( $ var1 ) !== get_class ( $ var2 ) ) { return false ; } $ obj1 = array_keys ( get_object_vars ( $ var1 ) ) ; $ obj2 = array_keys ( get_object_vars ( $ var2 ) ) ; //Look for an unused index in $var1 do { $ key = uniqid ( "is_ref_" , true ) ; } while ( in_array ( $ key , $ obj1 ) ) ; //The two variables differ in content ... They can't be the same if ( in_array ( $ key , $ obj2 ) ) { return false ; } //The arrays point to the same data if changes are reflected in $var2 $ data = uniqid ( "is_ref_data_" , true ) ; $ var1 -> $ key = & $ data ; //There seems to be a modification ... if ( isset ( $ var2 -> $ key ) ) { if ( $ var2 -> $ key === $ data ) { $ same = true ; } } //Undo our changes ... unset ( $ var1 -> $ key ) ; } elseif ( is_resource ( $ var1 ) ) { if ( get_resource_type ( $ var1 ) !== get_resource_type ( $ var2 ) ) { return false ; } return ( ( string ) $ var1 ) === ( ( string ) $ var2 ) ; } else { //Simple variables ... if ( $ var1 !== $ var2 ) { //Data mismatch ... They can't be the same ... return false ; } //To check for a reference of a variable with simple type //simply store its old value and check against modifications of the second variable ;-) do { $ key = uniqid ( "is_ref_" , true ) ; } while ( $ key === $ var1 ) ; $ tmp = $ var1 ; //WE NEED A COPY HERE!!! $ var1 = $ key ; //Set var1 to the value of $key (copy) $ same = $ var1 === $ var2 ; //Check if $var2 was modified too ... $ var1 = $ tmp ; //Undo our changes ... } return $ same ; }
Checks if two objects
are references of each
other

@param var1: array or object
@param var2: array or object
Checks if two objects are references of each other