【问题标题】:Symfony event addListener and variable Scope/StateSymfony 事件 addListener 和变量 Scope/State
【发布时间】:2014-03-28 18:56:46
【问题描述】:

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

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

提前致谢。

【问题讨论】:

    标签: php scope listener mediator


    【解决方案1】:

    由于您使用的是闭包函数,因此您需要指定要像这样“使用”$banner_holder 数组

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

    【讨论】:

    • 太好了,作为一名 java 程序员,做 php 让我头疼。不知道那个语法。
    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 2022-01-19
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 2016-09-11
    相关资源
    最近更新 更多