欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php闭包中使用use声明变量的作用域实例分析

程序员文章站 2022-06-05 16:18:49
本文实例讲述了php闭包中使用use声明变量的作用域。分享给大家供大家参考,具体如下:

本文实例讲述了php闭包中使用use声明变量的作用域。分享给大家供大家参考,具体如下:

<?php
function getclosure($i)
{
    $i = $i.'-'.date('h:i:s');
    return function ($param) use ($i) {
        echo "--- param: $param ---\n";
        echo "--- i: $i ---\n";
    };
}
$c = getclosure(123);
$i = 456;
$c('test');
sleep(3);
$c2 = getclosure(123);
$c2('test');
$c('test');
/*
output:
--- param: test ---
--- i: 123-21:36:52 ---
--- param: test ---
--- i: 123-21:36:55 ---
--- param: test ---
--- i: 123-21:36:52 ---
*/

如上,闭包中使用use声明的变量来自于生成闭包实例时所在作用域内的同名变量,而不是来自于运行闭包时所在作用域内的同名变量。

而闭包的函数参数则是和正常的函数参数一样来自于运行时所在作用域内的同名变量。

以下为opcode:

finding entry points
branch analysis from position: 0
jump found. position 1 = -2
filename:       /tmp/testclosure.php
function name:  (null)
number of ops:  20
compiled vars:  !0 = $c, !1 = $i, !2 = $c2
line     #* e i o op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  e >   nop
  11     1        send_val                                                 123
         2        do_fcall                                      1  $0      'getclosure'
         3        assign                                                   !0, $0
  12     4        assign                                                   !1, 456
  13     5        init_fcall_by_name                                       !0
         6        send_val                                                 'test'
         7        do_fcall_by_name                              1
  14     8        send_val                                                 3
         9        do_fcall                                      1          'sleep'
  15    10        send_val                                                 123
        11        do_fcall                                      1  $5      'getclosure'
        12        assign                                                   !2, $5
  16    13        init_fcall_by_name                                       !2
        14        send_val                                                 'test'
        15        do_fcall_by_name                              1
  17    16        init_fcall_by_name                                       !0
        17        send_val                                                 'test'
        18        do_fcall_by_name                              1
  29    19      > return                                                   1
function %00%7bclosure%7d%2ftmp%2ftestclosure.php0x7fb0115f505:
finding entry points
branch analysis from position: 0
jump found. position 1 = -2
filename:       /tmp/testclosure.php
function name:  {closure}
number of ops:  12
compiled vars:  !0 = $param, !1 = $i
line     #* e i o op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   5     0  e >   recv                                             !0
         1        fetch_r                      static              $0      'i'
         2        assign                                                   !1, $0
   6     3        add_string                                       ~2      '---+param%3a+'
         4        add_var                                          ~2      ~2, !0
         5        add_string                                       ~2      ~2, '+---%0a'
         6        echo                                                     ~2
   7     7        add_string                                       ~3      '---+i%3a+'
         8        add_var                                          ~3      ~3, !1
         9        add_string                                       ~3      ~3, '+---%0a'
        10        echo                                                     ~3
   8    11      > return                                                   null
end of function %00%7bclosure%7d%2ftmp%2ftestclosure.php0x7fb0115f505
function getclosure:
finding entry points
branch analysis from position: 0
jump found. position 1 = -2
filename:       /tmp/testclosure.php
function name:  getclosure
number of ops:  9
compiled vars:  !0 = $i
line     #* e i o op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   2     0  e >   recv                                             !0
   4     1        concat                                           ~0      !0, '-'
         2        send_val                                                 'h%3ai%3as'
         3        do_fcall                                      1  $1      'date'
         4        concat                                           ~2      ~0, $1
         5        assign                                                   !0, ~2
   5     6        declare_lambda_function                                  '%00%7bclosure%7d%2ftmp%2ftestclosure.php0x7fb0115f5051'
   8     7      > return                                                   ~4
   9     8*     > return                                                   null
end of function getclosure

如上,闭包函数的op_array(相当于类定义)在编译期完成,但在运行期生成闭包实例(相当于类实例)时会为不同实例绑定不同的use静态变量(在declare_lambda_function中完成)。

更多关于php相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。