colinmollenhour/credis 1.17 bug

复制代码
Call to a member function getMode() on null

vendor\colinmollenhour\credis\Client.php(783): Credis_Client->assertNotPipelineOrMulti('Credis_Client::...')

错误代码

php 复制代码
#Credis_Client vendor\colinmollenhour\credis\Client.php

   /**
     * @param ?int $Iterator
     * @param string $pattern
     * @param int $count
     * @return bool|array
     * @throws CredisException
     */
    public function scan(&$Iterator, $pattern = null, $count = null) {
        $this->assertNotPipelineOrMulti(__METHOD__);
        return $this->__call('scan', array(&$Iterator, $pattern, $count));
    }

    /**
     * @param string $caller
     * @return void
     * @throws CredisException
     */
    protected function assertNotPipelineOrMulti($caller) {
        if ($this->standalone && ($this->isMulti || $this->usePipeline) ||
            // phpredis triggers a php fatal error, so do the check before
            !$this->standalone && ($this->redis->getMode() === Redis::MULTI || $this->redis->getMode() === Redis::PIPELINE)) {
            throw new CredisException('multi()/pipeline() mode can not be used with ' . $caller);
        }
    }

报错是这个代码 "this-\>redis-\>getMode()",其中this->redis为空。

调用代码

php 复制代码
$client = new Credis_Client();
$r = $client->select(15);
$data = $client->scan($client, "keywords:*", 1);
var_dump($data);

此时代码正常

php 复制代码
$host = '127.0.0.1';
$port = 6379;
$timeout = null;
$persistent = '';
$db = 15;
$client = new Credis_Client($host,$port,$timeout ,$persistent,$db);

$data = $client->scan($client, "keywords:*", 1);
var_dump($data);
var_dump(123);

会报上面的错误,就是Credis_Client构造里$this->redis没初始化。

使用懒加载,都是调用方法之后实现连接,实例化$this->redis。

可以按照正常顺序调用,也可以改代码。

修改后代码

php 复制代码
protected function assertNotPipelineOrMulti($caller) {
        if (empty($this->redis)) {
            $this->connect();
        }
        if ($this->standalone && ($this->isMulti || $this->usePipeline) ||
            // phpredis triggers a php fatal error, so do the check before
            !$this->standalone && ($this->redis->getMode() === Redis::MULTI || $this->redis->getMode() === Redis::PIPELINE)) {
            throw new CredisException('multi()/pipeline() mode can not be used with ' . $caller);
        }
    }
相关推荐
Soari1 小时前
【紧急发布】Claude Code v2.1.148 :修复 Bash 127 瘫痪 Bug,/simplify 升级为 AI 代码评审
人工智能·bug·bash·claudecode
ABILI .1 天前
FineReport网络报表bug
bug·finereport·帆软
小陶来咯1 天前
机器人坐下后拍触摸板站起行动指令无效 — Bug 分析
机器人·bug
萤萤七悬2 天前
【AI精彩BUG汇总】一、yolo图像训练截图蓝色变橙色
人工智能·yolo·bug
小雨青年2 天前
Git Bisect 实战:用二分法快速找到引入 Bug 的提交
git·bug
淘矿人2 天前
Claude辅助DevOps实践
java·大数据·运维·人工智能·算法·bug·devops
哥本哈士奇3 天前
LangChain Deepagent 版本0.6.1中间件一个bug
中间件·langchain·bug
java修仙传3 天前
Java 实习日记:断面状态筛选 Bug 修复与对比案例日期过滤优化
java·bug·实习
CeshirenTester3 天前
当其他人回复您的帖子时是否接收实时通知? “线上Bug排查3小时,CTO当场发火”:一套让测试人“硬气”起来的质量保障体系
bug
139的世界真奇妙4 天前
生产问题排查记录
golang·bug·学习方法