- var_dump: documentation ( source)
- spl_object_hash: documentation ( source)
<?php
class ObjectMap
{
private $keyMap = [];
private $valMap = [];
private $keyVal = [];
private $valKey = [];
private function hash($object)
{
return spl_object_hash($object);
}
public function set($key, $val)
{
$this->keyMap[$keyHash = $this->hash($key)] = $key;
$this->valMap[$valHash = $this->hash($val)] = $val;
$this->keyVal[$keyHash] = $valHash;
$this->valKey[$valHash] = $keyHash;
}
public function get($key)
{
if (isset($this->keyVal[$keyHash = $this->hash($key)], $this->valMap[$this->keyVal[$keyHash]])) {
return $this->valMap[$this->keyVal[$keyHash]];
}
return null;
}
public function getKey($val)
{
if (isset($this->valKey[$valHash = $this->hash($val)], $this->keyMap[$this->valKey[$valHash]])) {
return $this->keyMap[$this->valKey[$valHash]];
}
}
}
$objectMap = new ObjectMap();
$objectMap->set($a = (object) ['value' => 'a'], $b = (object) ['value' => 'b']);
$objectMap->set($c = (object) ['value' => 'c'], $d = (object) ['value' => 'd']);
$objectMap->set($e = (object) ['value' => 'e'], $f = (object) ['value' => 'f']);
var_dump($objectMap->get($a));
var_dump($objectMap->get($c));
var_dump($objectMap->getKey($f));