使用 GatewayWorker + redis 使用出现问题

duoduo

使用 GatewayWorker + redis 使用出现问题,

我在 Events.php 中做如下处理

public static function onWorkerStart($businessWorker) 中初始化redis 

      $redis = new redis();
          $redis -> connect('127.0.0.1',6379);
          echo "启动redis\n";

public static function onMessage($client_id, $message) 中存储数据

         $redis->zAdd($key, $i, $redis_str);

但是启动后,显示如下错误:

PHP Notice:  Undefined variable: redis in /GatewayWorker/Applications/YourApp/Events.php on line 149

Notice: Undefined variable: redis in /GatewayWorker/Applications/YourApp/Events.php on line 149
PHP Fatal error:  Call to a member function zAdd() on a non-object in /GatewayWorker/Applications/YourApp/Events.php on line 149

Fatal error: Call to a member function zAdd() on a non-object in /GatewayWorker/Applications/YourApp/Events.php on line 149
WORKER EXIT UNEXPECTED E_ERROR Call to a member function zAdd() on a non-object in /GatewayWorker/Applications/YourApp/Events.php on line 149
worker exit with status 65280
启动redis

不晓得是何原因哦,偶是新手,请各位大哥多多指点,感激不尽。

7721 1 0
1个回答

damao

哈哈,php基础不过关。
给你个手册学习下
http://php.net/manual/zh/language.variables.scope.php

  • duoduo 2016-11-25

    大哥您好,实在是初学哦,我是看这里介绍的http://www.workerman.net/gatewaydoc/gateway-worker-development/onworkerstart.html
    正确的写法如何能否指点下,谢谢您。

  • fff 2016-11-25

    private $_redis;
    public static function onWorkerStart($businessWorker) {
    $redis = new redis();
    $redis -> connect('127.0.0.1',6379);
    $this->_redis=$redis;
    echo "启动redis\n";
    }

    public static function onMessage($client_id, $message) {
    $this->_redis->zAdd($key, $i, $redis_str);
    }

  • duoduo 2016-11-30

    @1424:您好,您这个也是有报错的。 静态方法中要用 self:: ,但是修改后也是有报错。郁闷了

  • damao 2016-12-03

    其实把上面手册那里基础看懂就会了

年代过于久远,无法发表回答
🔝