workerman客户端全局变量问题

CC.
<?php
$worker = new Worker();
$worker->onWorkerStart = function()
{
for ($i=1; $i<=10; $i++){
global  $data_connection;
$data_connection= $i;
$a = new AsyncTcpConnection('XXX');
// 建立连接
$con->onConnect = function($connection) {
    global  $data_connection;
$connection->send($data_connection);}
}
}
Worker::runAll();

$connection->send($data_connection)的时候 $data_connection无法取出正确值
怎么样赋值才可以全局变量

1225 1 0
1个回答

phpcreeper

将包含global关键字的行代码删除,然后onConnect回调改为:

$con->onConnect = function($connection)use($data_connection){
    $connection->send($data_connection);
};
  • 暂无评论
年代过于久远,无法发表回答
×
🔝