Symfony事件addListener和变量范围/状态


Symfony event addListener and variable Scope/State

我目前正在使用still/curl-easy来制作多curl并行请求,但是我在scope/state上遇到了一些困难:

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

…初始化队列选项

// 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的数组推到监听器之外?

由于您使用的是闭包函数,因此您需要指定您希望"使用"$banner_holder数组,如下所示

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