PHP匿名函数的写法

传统写法
<pre>
function timer () {
echo "hello world";
}
Swoole\Timer::tick(2000, 'timer');
</pre>
闭包写法
<pre>
Swoole\Timer::tick(2000, function () {
echo "hello world";
});
</pre>


高级点的
传统写法
<pre>
$str = "hello world";
function timer () {
global $str;
echo $str;
}
Swoole\Timer::tick(2000, 'timer');
</pre>
闭包写法
<pre>

$str = "hello world";
Swoole\Timer::tick(2000, function () use ($str) {
echo $str;
});
</pre>

猜你喜欢

转载自www.cnblogs.com/newmiracle/p/11875325.html