有点难以解释,但是从这个例子中应该可以清楚地知道我要做什么:
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?
答案 0 :(得分:0)
这里您正在调用一个函数,但是您已经在beginnig处写了一个美元符号,就好像它是一个变量:$sendClientToHost($sendThis);
。
只需删除“ $”,我认为应该没问题...
答案 1 :(得分:0)
我需要在其他函数中使用闭包。
$ sendObjects = function()use($ data,$ sendClientToHost){...};