I am currently using the following method of hashing resources for lookups:
$foo = socket_create(...);
$bar = socket_create(...);
$map[(int)$foo] = 'foo';
$map[(int)$bar] = 'bar';
echo $map[(int)$foo]; // "foo"
Is integer casting the best option for this? If not, what other hashing method would be better or more efficient? These lookups will be performed in a collection into the hundreds, many times per second in a tight loop (socket polling), so I've already ruled out iteration-based solutions.
Edit:
To explain my situation a little better, the socket_select() function takes arrays of socket resources by reference and modifies them such that after the function call, they will contain only the resources which have changed (e.g. ready to be read from). I use a Socket class as a wrapper for socket resources, to make my code more abstract and testable:
$socketObject = new Socket($socketResource);
Another of my classes keeps a list of all socket resources that need to be polled every time we call socket_select():
$reads = [$socketResource1, $socketResource2, ...];
socket_select($reads, null, null, 0);
After the call to socket_select(), I know which socket resources have changed, but to do anything meaningful in my code, I need to know which socket objects those resources correspond to. Thus, I need some way to map socket resources to their objects:
foreach ($reads as $socketResource) {
// Which socket object does $socketResource correspond to here?
// Currently, I use a solution like this:
$socketObject = $this->map[(int)$socketResource];
// Unfortunately, this behavior isn't guaranteed, so it isn't reliable...
}