0
我知道如何在setCallback函數外設置一個變量的值並在其中使用它。如何在symfony2中訪問setCallback函數之外的變量?
$response = new StreamedResponse();
$i = 0;
$params = "hello";
$response->setCallback(function() use ($params){
while($i < 999999){
echo 'Something';
$i = $i + 1;
}
});
即通過使用use
。
我想從這個回調函數中設置一個變量的值,並希望在函數外部使用它。我怎樣才能實現這一點,而不使用全局變量?
$response = new StreamedResponse();
$i = 0;
$params = "hello";
$response->setCallback(function() use ($params){
// --- Set variable here ---
while($i < 999999){
echo 'Something';
$i = $i + 1;
}
});
-- Use variable here ---
我想下面的代碼,但不工作 -
$response = new StreamedResponse();
$format = "json";
$response->setCallback(function() use(&$format) {
$format = "xml";
echo $format; //prints xml
});
echo $format; //prints json
感謝。如果我傳遞一個參數數組而不是單個變量,應該如何傳遞變量? – User42
如果您有多個參數,則必須在每個參數前加一個&符號:'... use(&$ one,&$ two,&$ three)'。無論參數是否是數組都不重要,只需使用它就像一個「正常」變量。 – hanzi
你的代碼工作正常,但這種技術不適合我。我正嘗試在StreamedResponse的setCallback()函數中設置一個變量的值。請確認。編輯了這個問題。 – User42