As I was writing unit tests against a new caching wrapper which extends the Memcached API, I ran across an issue when overriding the Memcached::get() method. According to PHP docs regarding the Memcached::get() method, there are three arguments which which must be added to my extended signature, which I added, but kept getting “…¬†should be compatible with that of Memcached::get()” errors. I tried looking for the method signature via source code but didn’t glean anything useful from the PECL documentation, so I turned to PHP’s RelectionClass to see if I could figure out what I was missing from my extending class signature which was causing the aforementioned errors. After a few minutes I ended up with the following code snippet:

$this->cache = Cache::factory(Cache::TYPE_VOLATILE);

$reflector = new ReflectionClass(get_class($this->cache));

foreach ($reflector->getMethod('get')->getParameters() as $param) {
    var_dump((string) $param);

Which outputted the following:

string(32) "Parameter #0 [ <required> $key ]"
string(37) "Parameter #1 [ <optional> $cache_cb ]"
string(39) "Parameter #2 [ <optional> &$cas_token ]"

After a few seconds investigating the output, I noticed that I wasn’t passing the third argument ($cas_token) by reference, but before I fixed my version I double checked the PHP docs regarding Memcached::get() and in fact noticed they indicated that $cas_token was indeed being passed by reference (as indicated by the ampersand &). After I altered my extended method to pass the third argument by reference, everything worked as expected. So if you ever need to introspect an API with little to no relevant documentation, try using PHP’s ReflectorClass to get the information you are after.