国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php閉包中使用use聲明變量的作用域?qū)嵗治?/h1>
2024-05-05 00:04:58
字體:
供稿:網(wǎng)友

本文實例講述了php閉包中使用use聲明變量的作用域。分享給大家供大家參考,具體如下:

<?phpfunction 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聲明的變量來自于生成閉包實例時所在作用域內(nèi)的同名變量,而不是來自于運行閉包時所在作用域內(nèi)的同名變量。

而閉包的函數(shù)參數(shù)則是和正常的函數(shù)參數(shù)一樣來自于運行時所在作用域內(nèi)的同名變量。

以下為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

如上,閉包函數(shù)的op_array(相當于類定義)在編譯期完成,但在運行期生成閉包實例(相當于類實例)時會為不同實例綁定不同的use靜態(tài)變量(在DECLARE_LAMBDA_FUNCTION中完成)。

希望本文所述對大家PHP程序設計有所幫助。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 东港市| 海安县| 依安县| 海门市| 漠河县| 惠东县| 商洛市| 松阳县| 天津市| 平舆县| 浦县| 新竹县| 宝丰县| 中宁县| 南雄市| 淄博市| 武平县| 马边| 安龙县| 都昌县| 高平市| 彭阳县| 遂平县| 安化县| 化德县| 商水县| 镇安县| 林周县| 普兰县| 丽江市| 屯门区| 琼结县| 运城市| 青冈县| 佛学| 社旗县| 金昌市| 留坝县| 修文县| 阿瓦提县| 伊春市|