Heim  >  Artikel  >  php教程  >  深入理解PHP中的匿名函数

深入理解PHP中的匿名函数

WBOY
WBOYOriginal
2016-06-21 08:50:59742Durchsuche

 

匿名函数的作用就是扩大函数的使用功能,在PHP 5.3以前,传递Callback的方式,我们只有俩种选择:

◆字符串的函数名

◆使用create_function的返回

在PHP5.3以后, 我们多了一个选择, 也就是Closure。


  1. 		<span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; ">$</span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit; ">func</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> = </span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit; ">function</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> () { ... };  </span></span>
  2. array_walk($arr, $func); 

从实现上来说, 第一种方式: 传递函数名字符串是最简单的。而第二种方式create_function, 其实和第一种方式本质上一样的, create_function返回一个字符串的函数名, 这个函数名的格式是:


  1. 		<span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">"\000_lambda_" . count(anonymous_functions)++ </span>
  2.  

我们来看看create_function的实现步骤:

1. 获取参数, 函数体;

2. 拼凑一个"function __lambda_func (参数) { 函数体;} "的字符串;

3. eval;

4. 通过__lambda_func在函数表中找到eval后得到的函数体, 找不到就出错;

5. 定义一个函数名:"\000_lambda_" . count(anonymous_functions)++;

6. 用新的函数名替换__lambda_func;

7. 返回新的函数。

我们来验证下:


  1. 		<span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; "><span class="tag" style="margin: 0px 0px 10px; padding: 7px 0px 4px; border: none; color: rgb(0, 102, 153); background-color: inherit; width: 635px; font-weight: bold; "></span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); background-color: inherit; font-weight: bold; ">php</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit; "> </span></span>
  2. create_function("", 'echo __FUNCTION__;');  
  3. call_user_func("\000lambda_1", 1);  
  4. ?> 
  5. //输出  
  6. __lambda_fun 

因为在eval的时候, 函数名是”__lambda_func”, 所以匿名函数内会输出__lambda_func, 而因为最后用”\000_lambda_”.count(anonymous_functions)++重命名了函数表中的”__lambda_func”函数, 所以可通过”\000_lambda_” . count(anonymous_functions)++调用这个匿名函数。为了证实这一点, 可以将create_function的返回值dump出来查看。

而在PHP 5.3发布的时候, 其中有一条new feature就是支持闭包/Lambda Function, 我第一反应是以为zval新增了一个IS_FUNCTION, 但实际上是构造了一个PHP 5.3引入的Closure”类”的实例, Closure类的构造函数是私有的, 所以不能被直接实例化, 另外Closure类是Final类, 所以也不能做为基类派生子类.


  1. 		<span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">//php-5.3.0  </span>
  2. $class = new ReflectionClass("Closure");  
  3. var_dump($class->isInternal());  
  4. var_dump($class->isAbstract() );  
  5. var_dump($class->isFinal());  
  6. var_dump($class->isInterface());  
  7. //输出:  
  8. bool(true)  
  9. bool(false)  
  10. bool(true)  
  11. bool(false)  
  12. ?> 

而PHP 5.3中对闭包的支持, 也仅仅是把要保持的外部变量, 做为Closure对象的”Static属性”(并不是普通意义上的可遍历/访问的属性).


  1. 		<span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit; ">//php-5.3.0  </span>
  2. $b = "laruence";  
  3. $func = function($a) use($b) {};  
  4. var_dump($func);  
  5. /* 输出:  
  6. object(Closure)#1 (2) {  
  7. ["static"]=> 
  8. array(1) {  
  9. ["b"]=> 
  10. string(8) "laruence"  
  11. }  
  12. ["parameter"]=> 
  13. array(1) {  
  14. ["$a"]=> 
  15. string(10) "required>"  
  16. }  
  17. }  
  18. */ 

这个实现, 个人认为和JS对闭包的支持比起来, 还是有些太简陋了。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn