怎么调用timer的参数

callplay
<?php
require_once _DIR_ . '/Workerman/Autoloader.php';
use Workerman\Worker;
use Workerman\Lib\Timer;

$w = new \Workerman\Worker(); 
$w->onWorkerStart=function(){ 
   $timer_id=Timer::add(1, function ($client_id) {
         var_dump(time() . " " . $client_id);
},,true);

     Timer::add(10,function()use($timer_id){
           var_dump('check timer id : '.$timer_id);
            var_dump('check client id : '.$client_id); **<- 怎么把$client_id 值放这个地方**
      },,false);
}; 
?>
2717 1 0
1个回答

phpcreeper

试试:

$worker = new Worker(); 
$worker->onWorkerStart = function($worker){ 
    //assume $client_id = 100
    Timer::add(1, function($client_id = 100)use($worker){
        $worker->client_id = $client_id;
    }, array(), true);

    Timer::add(2, function()use($worker){
        var_dump('get client id : '. $worker->client_id);
    }, [], false);
}; 
  • 暂无评论
年代过于久远,无法发表回答
×
🔝