本篇文章给大家带来的内容是关于ThinkPHP 5.1修改Cache 源码的方法介绍(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
最近在学习 THinkPHP 5.1,看了 Cache 方法的操作,有一点疑惑。虽然封装了很多方法,使用的时候很方便,但是对 Redis 的高级操作好像不是很友好,出于学习的目的,对源码进行了一点小修改。首先声明两点:一是此次的修改,只是个人观点,不适于所有人;二是此次修改仅为学习所用,各位谨慎修改源码。
问题
在练习 redis 的时候,发现如果想要使用高级方法,例如 hset、hget 等,要先返回句柄,然后才能执行。如下
<?php
namespace app\index\controller;
use think\cache\driver\Redis;
use think\Controller;
class RedisTest extends Controller
{
public function index()
{
$redis = new Redis();
$redis = $redis->handler();
dump($redis->hSet('h_name', '1', 'tom'));// int(1)
}
}可以看到,执行成功。问题是为什么要先返回句柄,可以用 __call 这种魔术方法来解决的。
追踪源码
既然有了疑惑,就要解惑。追踪着源码,看到 thinkphp/library/think/cache/Driver.php,发现确实没有 __call,只是 handler 来返回句柄来执行高级方法。没想明白为什么不用 __clss。
解决问题
解决方法就是在 thinkphp/library/think/cache/Driver.php 中添加 __call 方法,这样不止 Redis 可以直接使用高级方法,其他继承此文件的 Cache 类都可以直接使用。代码如下
/**
* 执行高级方法
* @param $method
* @param $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return call_user_func_array(array($this->handler(), $method), $parameters);
}再看下测试代码
立即学习“PHP免费学习笔记(深入)”;
<?php
namespace app\index\controller;
use think\cache\driver\Redis;
use think\Controller;
class RedisTest extends Controller
{
public function index()
{
$redis = new Redis();
// $redis = $redis->handler();
dump($redis->hSet('h_name', '2', 'jerry'));// int(1)
}
}到此问题已解决。当我修改完的时候,想起 Laravel 似乎就是用的 __call,然后去看了源码,确实如此。在 ravel/vendor/laravel/framework/src/Illuminate/Redis/RedisManager.php 中有如下代码
/**
* Pass methods onto the default Redis connection.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->connection()->{$method}(...$parameters);
}结语
其实这次小修改的象征意义大于实际意义,毕竟这不是什么 bug,使用 handler 也是可以实现的。对我来说更大的意义是,遇到些问题会更倾向于查看源码。看得多了,自然能力会提升。
以上就是ThinkPHP 5.1修改Cache 源码的方法介绍(代码示例)的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号