Redis Credis_Client Php 库

Redis Credis_Client Php library

我正在为我的一个应用程序使用 Php Credis_Client 库。它以类似的方式定义了所有的redis命令。

虽然调用这些函数可以很好地从 Redis 存储和检索数据。

我浏览了库代码以检查它到底做了什么。但是我无法弄清楚它是如何工作的?

代码如下:

我用来设置散列键的函数,

hSet('test','field','value');

这是我在 Redis lib 文件中看到的

 * Hashes:
 * @method bool|int      hSet(string $key, string $field, string $value)
 * @method bool          hSetNx(string $key, string $field, string $value)

并在 __call($name, $args) 函数中

$response = call_user_func_array(array($this->redis, $name), $args);
//where $name can be function name and $args is parameters to be passed

但是无法找出 hSet 函数写在 php 中的位置。 如有任何帮助或建议,我们将不胜感激。

__call() 方法是 class 中非显式定义方法的回退:当您尝试使用不存在的 class 方法时调用它。

即:

cass A {
   public function x1() { return 1; }
   public function x2() { return 2; }
   public function __call($name, $args) { return 3; }
}

$a = new A;

var_dump($a->nonExistingMethod(1,2,3));

这显示 3.

__call方法也接收2个参数,第一个是你调用的不存在函数的名称,第二个是一个参数数组,

在前面的示例中,$name 将是 nonExistingMethod,而 $args 将是 array( 1, 2, 3 )

在你的例子中,当你调用 hSet 时,它回退到 __call 方法,使用 'hset' 作为名称和 array('field','value') 作为参数,导致 $this->redis->hSet('field','value')