PHP xxx=function(){} 这种写法是什么意思?

WBOY
Release: 2016-06-06 20:11:08
Original
1078 people have browsed it

看wokman这个框架的时候,发现有下面这些代码 http://doc3.workerman.net/worker-development/add.html

<code>$task = new Worker();
$task->onWorkerStart = function($task)
{
    $to = 'workerman@workerman.net';
    $content = 'hello workerman';
    // 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次
    Timer::add(10, 'send_mail', array($to, $content), false);
};</code>
Copy after login
Copy after login

xxx=function(){}在JS里面倒是常见,但在PHP里面我还是第一次见,不明白其中,望指点

回复内容:

看wokman这个框架的时候,发现有下面这些代码 http://doc3.workerman.net/worker-development/add.html

<code>$task = new Worker();
$task->onWorkerStart = function($task)
{
    $to = 'workerman@workerman.net';
    $content = 'hello workerman';
    // 10秒后执行发送邮件任务,最后一个参数传递false,表示只运行一次
    Timer::add(10, 'send_mail', array($to, $content), false);
};</code>
Copy after login
Copy after login

xxx=function(){}在JS里面倒是常见,但在PHP里面我还是第一次见,不明白其中,望指点

匿名函数

匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况。

http://www.php.net/manual/zh/functions.anonymous.php

与Js类似,这是PHP的匿名函数的使用。

传说中的匿名函数

和python里面lamda函数类似

Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!