这有点难以解释,但从这个例子中应该可以看出我想做什么。
public function onMessage($data)
{
if (!function_exists("sendClientToHost")) {
$sendClientToHost = function(&$sendThis) use($type) {
$sendThis += array('type' => $type);
//.... execute code ....//
};
}
if (!function_exists("sendObjects")) {
$sendObjects = function() use($data) {
$user_id = $data->user_id;
$sendThis = array(
"msg" => $user_id,);
$sendClientToHost($sendThis);
};
}
$sendObjects();
}
我试着在OnMessage函数中调用内部函数sendObjects,这部分工作正常。但是,sendObjects函数有一个关键的错误,它说sendClientToHost是未定义的。如何在sendObjects函数内部调用sendClientToHost?
这里你是在调用一个函数,但你在开头写了一个美元符号,好像它是一个变量。$sendClientToHost($sendThis);
.
只要删除"$",我想应该就可以了......
我需要在其他函数中使用闭包。
$sendObjects = function() use($data, $sendClientToHost) {...};