How to compute a unique hash for a callable
php
Solution
You can always cast to object for hashing purposes:
<?php
class Foo{
public function __construct(){
$foo = array($this,'memberFunc');
var_dump( spl_object_hash((object)$foo) );
var_dump( spl_object_hash((object)$foo) );
}
}
new Foo;
string(32) "00000000532ba9fd000000000cc9b0a5"
string(32) "00000000532ba9fd000000000cc9b0a5"
Problem
I'm trying to write a memoization function, and I just realized this solution does not work when the callback is not a simple string. For example, PHP can accept a callback in the form `array($this,'memberFunc')`, which is not amenable to serialization. Then I realized that we don't really want to hash/serialize the whole callback function/object anyway, we just need a unique ID for it so we can check for reference equality. I thought `spl_object_hash` would do the trick, but it doesn't work on arrays. Is there another way to generate a unique reference ID for a callable?