0

I'm working on a PHP class that serves as a wrapper for the Memcached class provided via a PECL extension. I am having difficulty handling a method that calls the get method. The third argument in the get method defaults to null and passes a variable by reference. I am having difficulties mirroring the functionality of the Memcached get method with my wrapper.

By using the Memcached class directly, the following occurs:

$m = new Memcached();
$m->addServer( '127.0.0.1', 11211 );

$m->add( 'my-key', 'my-value' );

$m->get( 'my-key', null, $cas_token );

var_dump( $cas_token ); // (float) 3212

The point here is that I can pass an uninitialized, null var to the get function and it works by setting the CAS token to that var, which can then later be accessed.

In my class, I want to offer the ability to use this CAS token with my wrapper method; however, I want the ability to do something different depending on whether the method is called with the third variable or not. I cannot find a way to differentiate whether the third variable has been explicitly called or not when I use the wrapper method. A skeleton of what I'm attempting follows:

class MyMemcached {

    ...

    public function get( $key, $cache_cb = NULL, &$cas_token = NULL ) {
        if ( is_callable( $cache_cb ) || ! is_null( $cas_token ) ) {
            $this->m->get( $key, $cache_cb, $cas_token );
        } else {
            // Do something differently
        }
    }
}

$myMemcached = new Memcached();
$myMemcached->addServer( '127.0.0.1', 11211 );

$myMemcached->add( 'my-key', 'my-value' );

$myMemcached->get( 'my-key', null, $cas_token );

var_dump( $cas_token ); // NULL

Again, in MyMemcached::get, I cannot distinguish between if a user calls it with a NULL var or not. Do you know of any way I can distinguish between these two events?

Thanks!

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
tollmanz
  • 3,113
  • 4
  • 29
  • 34

1 Answers1

2

http://www.php.net/manual/en/function.func-num-args.php

You can determine by this function whatever there was 2 or 3 arguments passed in function. If number is 3, than actual null value has been passed, and if not, there is only two parameters passed in function.

toske
  • 1,744
  • 13
  • 24