使用匿名函数作为参数访问外部变量

基本上,我使用这个方便的函数来处理 db 行(关注 PDO 和/或其他东西)

function fetch($query,$func) {
$query = mysql_query($query);
while($r = mysql_fetch_assoc($query)) {
$func($r);
}
}

有了这个函数,我可以简单地做:

fetch("SELECT title FROM tbl", function($r){
//> $r['title'] contains the title
});

现在我需要在 var 中连接所有的 $r['title'](这只是一个例子)。

我怎么能那么做呢? 我是这么想的,但它不是很优雅:

$result = '';
fetch("SELECT title FROM tbl", function($r){
global $result;
$result .= $r['title'];
});


echo $result;
41685 次浏览

You have to use use as described in docs:

Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing.

Code:

$result = '';
fetch("SELECT title FROM tbl", function($r) use (&$result) {
$result .= $r['title'];
});

But beware (taken from one of comments in previous link):

use() parameters are early binding - they use the variable's value at the point where the lambda function is declared, rather than the point where the lambda function is called (late binding).

What about rewriting 'fetch' to call $func only once ?

function fetch($query,$func) {
$query = mysql_query($query);
$retVal = array();
while($r = mysql_fetch_assoc($query)) {
$retVal[] = $r;
}
$func($retVal);
}

This way you would call $func only once and re-process the array once fetched? Not sure about the performance even tho calling 200 times a function doesn't sound like a good idea.