Reputation: 3132
My problem is I want to map by object equality (==), not unique instances. In other words, refactor this to not throw an error and return the mapped value:
$var1 = (object) [1,2,10];
$var2 = (object) [1,2,10];
$objmap = new SplObjectStorage();
$objmap[$var1] = "mapped value here";
echo $objmap[$var2];
Edit: Also, if I end up needing to iterate over the keys of the SplObjectStorage object to get by equality, I'd like instead to know a good way to just hash objects/arrays and simply map to the hashes.
Upvotes: 0
Views: 337
Reputation: 173642
I would implement a hashing function for the object contents; something simple like this:
function getHash($obj)
{
return md5(serialize($obj));
}
$objmap = array();
$objmap[getHash($var1)] = "mapped value here";
echo $objmap[getHash($var2)];
Upvotes: 1