使用 GatewayWorker + redis 使用出現(xiàn)問題,
我在 Events.php 中做如下處理
public static function onWorkerStart($businessWorker) 中初始化redis
$redis = new redis();
$redis -> connect('127.0.0.1',6379);
echo "啟動(dòng)redis\n";
public static function onMessage($client_id, $message) 中存儲(chǔ)數(shù)據(jù)
$redis->zAdd($key, $i, $redis_str);
但是啟動(dòng)后,顯示如下錯(cuò)誤:
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
啟動(dòng)redis
不曉得是何原因哦,偶是新手,請(qǐng)各位大哥多多指點(diǎn),感激不盡。
哈哈,php基礎(chǔ)不過關(guān)。
給你個(gè)手冊(cè)學(xué)習(xí)下
http://php.net/manual/zh/language.variables.scope.php
大哥您好,實(shí)在是初學(xué)哦,我是看這里介紹的http://www.wtbis.cn/gatewaydoc/gateway-worker-development/onworkerstart.html
正確的寫法如何能否指點(diǎn)下,謝謝您。
private $_redis;
public static function onWorkerStart($businessWorker) {
$redis = new redis();
$redis -> connect('127.0.0.1',6379);
$this->_redis=$redis;
echo "啟動(dòng)redis\n";
}
public static function onMessage($client_id, $message) {
$this->_redis->zAdd($key, $i, $redis_str);
}