在 PHP 中,如何从同一个函数中的独立内部函数中调用一个内部函数?

问题描述 投票:0回答:2

这有点难以解释,但从这个例子中应该可以看出我想做什么。


    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?

php function methods closures
2个回答
0
投票

这里你是在调用一个函数,但你在开头写了一个美元符号,好像它是一个变量。$sendClientToHost($sendThis);.

只要删除"$",我想应该就可以了......


0
投票

我需要在其他函数中使用闭包。

$sendObjects = function() use($data, $sendClientToHost) {...};

© www.soinside.com 2019 - 2024. All rights reserved.