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

What does use mean after function in WorkerMan?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-04 09:19:47
Original
1449 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template