Symfony事件addListener和变量Scope / State

时间:2014-03-28 18:56:46

标签: php scope listener mediator

我目前正在使用sitl / curl-easy进行多卷曲并行请求,但我在范围/状态方面遇到了一些困难:

// Init queue of requests
$queue = new \cURL\RequestsQueue;

... init队列选项

// Set function to be executed when request will be completed
$queue->addListener('complete', function (\cURL\Event $event) 
{
    $response = $event->response->getContent();

    // ugly :D, get the key from the object request
    $key = explode("&key=", array_values($event->request->getOptions()->toArray())[0])[1];

    // if response is not null, truncate it to use less space 
    if ($response != null){
        $response = str_replace(array("\r", "\n"), "", $response);
    }

    >>>>>>>> DON'T WORK, $banner_holder is declared on the top of the php page
    array_push($banner_holder, array("key"=> $key,"content"=>$response));
});

如何在监听器之外推送$ key和$ response数组?

提前致谢。

1 个答案:

答案 0 :(得分:1)

由于您正在使用闭包函数,因此您需要指定要“使用”$banner_holder数组,如此

$queue->addListener('complete', function (\cURL\Event $event) use ($banner_holder) {