您现在正在浏览:首页 > 文章资讯 > 我的日志

文章关键词测试

作者:时间:2016-07-24来源:阅读:80
<p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">不知不觉发现PHP已经出到了5.5版本,而自己一直在用PHP5.2,让我看起来像深山出来的小伙子一样,又土又落后。在我习惯在javascript中使用闭包之后,忽然间对PHP的闭包打起了兴趣。</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">于是乎在网上下了个WAMP集成开发环境,是PHP5.3版本的(PHP5.3开始引入了闭包的特性),不得不说WAMP安装使用真的很方便。简单配置了一下,开始动手。</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">&nbsp;</p><p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px;"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px;"></span></p><h1 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 3rem; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">匿名函数</h1><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">提到闭包就不得不想起匿名函数,也叫闭包函数(closures),貌似PHP闭包实现主要就是靠它。声明一个匿名函数是这样:</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;"><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 10pt; line-height: 1.5;"></span></p><pre class="brush:php; toolbar: true; auto-links: false;" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; border-radius: 3px; padding: 2px; line-height: 1.4; word-wrap: normal; color: rgb(119, 119, 119); background-color: rgb(253, 246, 227);">$func&nbsp;=&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp; };&nbsp;//带结束符</pre><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;"><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 10pt; line-height: 1.5;">可以看到,匿名函数因为没有名字,如果要使用它,需要将其返回给一个变量。匿名函数也像普通函数一样可以声明参数,调用方法也相同:</span></p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;"><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 10pt; line-height: 1.5;"><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent;"></span></span></p><pre class="brush:php; toolbar: true; auto-links: false;" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; border-radius: 3px; padding: 2px; line-height: 1.4; word-wrap: normal; color: rgb(119, 119, 119); background-color: rgb(253, 246, 227);">$func&nbsp;=&nbsp;function(&nbsp;$param&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$param; }; $func(&nbsp;&#39;some&nbsp;string&#39;&nbsp;);//输出://some&nbsp;string</pre><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;"><br/></p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">顺便提一下,PHP在引入闭包之前,也有一个可以创建匿名函数的函数:create function,但是代码逻辑只能写成字符串,这样看起来很晦涩并且不好维护,所以很少有人用。</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">&nbsp;</p><p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px;"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px;"></span></p><h1 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 3rem; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">实现闭包</h1><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">将匿名函数在普通函数中当做参数传入,也可以被返回。这就实现了一个简单的闭包。</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">下边有三个例子</p><pre class="brush:php; toolbar: true; auto-links: false;" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; border-radius: 3px; padding: 2px; line-height: 1.4; word-wrap: normal; color: rgb(119, 119, 119); background-color: rgb(253, 246, 227);">//例一//在函数里定义一个匿名函数,并且调用它function&nbsp;printStr()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$func&nbsp;=&nbsp;function(&nbsp;$str&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$str; &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;$func(&nbsp;&#39;some&nbsp;string&#39;&nbsp;); } printStr();//例二//在函数中把匿名函数返回,并且调用它function&nbsp;getPrintStrFunc()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$func&nbsp;=&nbsp;function(&nbsp;$str&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$str; &nbsp;&nbsp;&nbsp;&nbsp;};&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$func; } $printStrFunc&nbsp;=&nbsp;getPrintStrFunc(); $printStrFunc(&nbsp;&#39;some&nbsp;string&#39;&nbsp;);//例三//把匿名函数当做参数传递,并且调用它function&nbsp;callFunc(&nbsp;$func&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$func(&nbsp;&#39;some&nbsp;string&#39;&nbsp;); } $printStrFunc&nbsp;=&nbsp;function(&nbsp;$str&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$str; }; callFunc(&nbsp;$printStrFunc&nbsp;);//也可以直接将匿名函数进行传递。如果你了解js,这种写法可能会很熟悉callFunc(&nbsp;function(&nbsp;$str&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$str; }&nbsp;);</pre><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;"><br/></p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">&nbsp;</p><p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px;"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px;"></span></p><h1 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 3rem; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">连接闭包和外界变量的关键字:USE</h1><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">闭包可以保存所在代码块上下文的一些变量和值。PHP在默认情况下,匿名函数不能调用所在代码块的上下文变量,而需要通过使用use关键字。</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">换一个例子看看:</p><pre class="brush:php; toolbar: true; auto-links: false;" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; border-radius: 3px; padding: 2px; line-height: 1.4; word-wrap: normal; color: rgb(119, 119, 119); background-color: rgb(253, 246, 227);">function&nbsp;getMoney()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$rmb&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;$dollar&nbsp;=&nbsp;6; &nbsp;&nbsp;&nbsp;&nbsp;$func&nbsp;=&nbsp;function()&nbsp;use&nbsp;(&nbsp;$rmb&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$rmb;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$dollar; &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;$func(); } getMoney();//输出://1//报错,找不到dorllar变量</pre><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">可以看到,dollar没有在use关键字中声明,在这个匿名函数里也就不能获取到它,所以开发中要注意这个问题。</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">有人可能会想到,是否可以在匿名函数中改变上下文的变量,但我发现是不可以的:</p><pre class="brush:php; toolbar: true; auto-links: false;" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; border-radius: 3px; padding: 2px; line-height: 1.4; word-wrap: normal; color: rgb(119, 119, 119); background-color: rgb(253, 246, 227);">function&nbsp;getMoney()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$rmb&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;$func&nbsp;=&nbsp;function()&nbsp;use&nbsp;(&nbsp;$rmb&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$rmb;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//把$rmb的值加1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$rmb++; &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;$func();&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$rmb; } getMoney();//输出://1//1</pre><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">啊,原来use所引用的也只不过是变量的一个副本而已。但是我想要完全引用变量,而不是复制。</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">要达到这种效果,其实在变量前加一个 &amp; 符号就可以了:</p><pre class="brush:php; toolbar: true; auto-links: false;" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; border-radius: 3px; padding: 2px; line-height: 1.4; word-wrap: normal; color: rgb(119, 119, 119); background-color: rgb(253, 246, 227);">function&nbsp;getMoney()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$rmb&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;$func&nbsp;=&nbsp;function()&nbsp;use&nbsp;(&nbsp;&amp;$rmb&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$rmb;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//把$rmb的值加1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$rmb++; &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;$func();&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$rmb; } getMoney();//输出://1//2</pre><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">好,这样匿名函数就可以引用上下文的变量了。如果将匿名函数返回给外界,匿名函数会保存use所引用的变量,而外界则不能得到这些变量,这样形成‘闭包’这个概念可能会更清晰一些。</p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">根据描述改变一下上面的例子:</p><pre class="brush:php; toolbar: true; auto-links: false;" style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; overflow-x: auto; border-radius: 3px; padding: 2px; line-height: 1.4; word-wrap: normal; color: rgb(119, 119, 119); background-color: rgb(253, 246, 227);">function&nbsp;getMoneyFunc()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$rmb&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;$func&nbsp;=&nbsp;function()&nbsp;use&nbsp;(&nbsp;&amp;$rmb&nbsp;)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;$rmb;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//把$rmb的值加1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$rmb++; &nbsp;&nbsp;&nbsp;&nbsp;};&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$func; } $getMoney&nbsp;=&nbsp;getMoneyFunc(); $getMoney(); $getMoney(); $getMoney();//输出://1//2//3</pre><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;"><br/></p><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">&nbsp;</p><p><span style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px;"></span><span style="color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px;"></span></p><h1 style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; font-size: 3rem; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">总结</h1><p style="box-sizing: inherit; -webkit-tap-highlight-color: transparent; margin-bottom: 16px; color: rgb(61, 70, 77); font-family: &quot;Lantinghei SC&quot;, &quot;Open Sans&quot;, Arial, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, STHeiti, &quot;WenQuanYi Micro Hei&quot;, SimSun, sans-serif; line-height: 30px; white-space: normal;">PHP闭包的特性并没有太大惊喜,其实用CLASS就可以实现类似甚至强大得多的功能,更不能和js的闭包相提并论,只能期待PHP以后对闭包支持的改进。不过匿名函数还是挺有用的,比如在使用preg_replace_callback等之类的函数可以不用在外部声明回调函数了。</p>
  • 站长资讯刊登此文只为传递信息,并不表示赞同或者反对作者观点。
  • 如果此内容给您造成了负面影响或者损失,本站不承担任何责任。
  • 如果内容涉及版权问题,请及时与我们取得联系。
100

相关文章