Weedpacket wrote:You could wiggle one variable by making some pissant change to its value (rather, a change to the value's elements or properties), and see if the change is reflected in the other variable (then change it back), but that sounds like a nice way to get into grief.
I think you may be right about this being the only way. I wrote a function this morning to do it, hopefully safely. This wouldn't be thread-safe, but I don't think that matters in PHP4. I've posted it here for use by anyone else that wants it, please forward on any comments, suggested improvements, etc.
/**
* is_equal
*
* Check if two objects are identical ( like === operator ).
* Composite types (objects and arrays) are compared by reference instead of by value.
*
* LICENCE:
*
* Free for any use provided the copyright notice is not removed from the source code.
* Provided "as-is" with no warranty, express or implied.
*
* @version 1.0
* @author Robert Tweed <robert@killingmoon.com>
* @copyright Copyright © Robert Tweed, 2005
* @link http://www.killingmoon.com/
*
* @param mixed $a First operand for comparison.
* @param mixed $b Second operand for comparison.
* @return boolean True only if inputs are identical.
*
*/
function is_equal( &$a, &$b ) {
//-- Make sure both operands are same type
$array = false;
if( is_array( $a ) )
$array = true;
//-- One is an array, other must be an array too
if( $array ) {
if( ! is_array( $b ) )
return false;
}
//-- If either operand is a primitive type, do a simple comparison
// otherwise both are objects
else if( ! is_object( $a ) || ! is_object( $b ) ) {
return ( $a === $b );
}
//-- Generate a unique key that can be safely added to both operands
$key = '__TESTKEY__';
while(
array_key_exists( $key, $a )
|| array_key_exists( $key, $b )
) {
$key .= '_';
}
//-- Set the test key in first operand
if( $array )
$a[ $key ] = true;
else
$a->$key = true;
//-- Check if key now exists in second operand
if( array_key_exists( $key, $b ) )
$eq = true;
else
$eq = false;
//-- Remove key that was added earlier
if( $array )
unset( $a[ $key ] );
else
unset( $a->$key );
//-- Return the result
return $eq;
}