php匿名函数
来源:互联网 发布:quest bar 知乎 编辑:程序博客网 时间:2024/06/05 15:08
匿名函数的简单例子:
<?php
$test1 = function($a){
echo $a;
};
$test1(2);
echo "\n";
$test1(3);
?>
输出 2 3
搭配use,继承父作用域的变量。使用use要注意父作用域变量的位置,必须在定义匿名函数之前,不然则会爆出“PHP Notice”
$b = "father";
$test2 = function($a) use ($b){
echo $a." ".$b;
};
$test2("I love");
输出 I love father
use继承并使用引用&
$b = 1;
$test3 = function($a) use (&$b){
echo $a * $b;
$b++;
};
$test3(2);
echo "\n";
$test3(2);
输出: 2 4
在外部重新定义$b,结果也是一样
<?php$b = 1;
$test3 = function($a) use (&$b){
echo $a * $b;
};
$test3(2);
$b = 4;
echo "\n";
$test3(2);
?>
输出: 2 4
类中定义匿名函数
<?php
class D{
protected $num = 2;
public function test4(){
$func = function(){
echo $this->num++;
};
return $func();
}
}
$Dtest = new D();
$Dtest->test4();
echo "\n";
$Dtest->test4();
?>
输出:2 3
阅读全文
0 0
- 61. PHP 匿名函数
- PHP匿名函数
- PHP的匿名函数
- php匿名函数
- PHP closure 匿名函数
- php 匿名函数
- php匿名函数
- PHP匿名函数
- php 匿名函数
- 【PHP】匿名函数
- PHP之匿名函数
- php 匿名函数
- php匿名函数
- php匿名函数的用法
- php手册之匿名函数
- PHP匿名函数的细节
- PHP匿名函数的写法
- PHP Closure创建匿名函数
- Android开发:Handler异步通信机制全面解析(包含Looper、Message Queue)
- mysql
- poj 3150 Cellular Automaton(迷糊,但原理是用的快速幂)
- Android优化UI(二)
- 实现通过桥梁ActivityB实现Activity A 启动Activity C
- php匿名函数
- React Native 解决Text在iOS、android中更改字体变大,导致项目文字变大布局错乱
- JQ html标签动态拼接的技巧
- RSA加密算法
- 官网 elastic 简单的搜索使用
- scrapy初始第二波——爬取知乎首页的问题和回答并写入mysql中
- php工作原理
- 程序在计算机内部是如何运行的?
- yum无法使用