Home >Backend Development >PHP Tutorial >What does use mean after function in WorkerMan?

What does use mean after function in WorkerMan?

WBOY
WBOYOriginal
2016-08-04 09:19:471442browse

For example, the example given has this piece of code:

<code>$recv_worker->onMessage = function($connection, $data)use($recv_worker)
{
    foreach($recv_worker->sendWorker->connections as $send_connection)
    {
        //$send_connection->websocketType = "\x82";
        $send_connection->send($data);
    }
};

这里的:function($connection, $data)use($recv_worker)
为什么function后面可以跟use,而不是直接写函数体{}?

另外,...=function(){} 这种回调函数是怎么实现的?只要js中看到有类似的用法,但在php中以前好像没有看到这种用法。
</code>

Reply content:

For example, the example given has this piece of code:

<code>$recv_worker->onMessage = function($connection, $data)use($recv_worker)
{
    foreach($recv_worker->sendWorker->connections as $send_connection)
    {
        //$send_connection->websocketType = "\x82";
        $send_connection->send($data);
    }
};

这里的:function($connection, $data)use($recv_worker)
为什么function后面可以跟use,而不是直接写函数体{}?

另外,...=function(){} 这种回调函数是怎么实现的?只要js中看到有类似的用法,但在php中以前好像没有看到这种用法。
</code>

That thing is called an anonymous function
http://www.php.net/manual/zh/...
You can read the official description first...it is indeed common in js...this is a feature after php5.3
use is used to pass external output to anonymous functions or closures

Maybe there are not many web frameworks that the subject is exposed to that use this kind of place...

php closure

A very commonly used feature now

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn