Key*_*eyo 10 php closures scope
显然,$ pid超出了范围.它不应该与功能"关闭"吗?我很确定这就是闭包在javascript中的工作原理.
根据一些文章php关闭被破坏,所以我无法访问这个?
那么如何从这个闭包函数中访问$ pid呢?
class MyClass {
static function getHdvdsCol($pid) {
$col = new PointColumn();
$col->key = $pid;
$col->parser = function($row) {
print $pid; // Undefined variable: pid
};
return $col;
}
}
$func = MyClass::getHdvdsCol(45);
call_user_func($func, $row);
Run Code Online (Sandbox Code Playgroud)
编辑我用它来解决它:$col->parser = function($row) use($pid).不过我觉得这很难看.
zer*_*kms 25
您需要指定以这种方式关闭哪些变量:
function($row) use ($pid) { ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6186 次 |
| 最近记录: |