php閉包中使用use聲明變量的作用域?qū)嵗治?/h1>
更新時(shí)間:2018年08月09日 11:55:32 作者:flynetcn
這篇文章主要介紹了php閉包中使用use聲明變量的作用域,結(jié)合實(shí)例形式分析了閉包中use聲明變量作用域的相關(guān)原理與分析,需要的朋友可以參考下
本文實(shí)例講述了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
聲明的變量來自于生成閉包實(shí)例時(shí)所在作用域內(nèi)的同名變量,而不是來自于運(yùn)行閉包時(shí)所在作用域內(nèi)的同名變量。
而閉包的函數(shù)參數(shù)則是和正常的函數(shù)參數(shù)一樣來自于運(yùn)行時(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
(相當(dāng)于類定義)在編譯期完成,但在運(yùn)行期生成閉包實(shí)例(相當(dāng)于類實(shí)例)時(shí)會(huì)為不同實(shí)例綁定不同的use
靜態(tài)變量(在DECLARE_LAMBDA_FUNCTION中完成)。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
-
The specified CGI application misbehaved by not returning a
The specified CGI application misbehaved by not returning a complete set of HTTP headers 2011-03-03
-
php使用array_search函數(shù)實(shí)現(xiàn)數(shù)組查找的方法
這篇文章主要介紹了php使用array_search函數(shù)實(shí)現(xiàn)數(shù)組查找的方法,涉及php數(shù)組查找的相關(guān)技巧,需要的朋友可以參考下 2015-06-06
-
因str_replace導(dǎo)致的注入問題總結(jié)
這篇文章主要給大家介紹了關(guān)于因str_replace導(dǎo)致的注入問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧 2019-08-08
-
php mssql擴(kuò)展SQL查詢中文字段名解決方法
問題就出現(xiàn)在SQLServer中表的字段名是中文,寫好的查詢語(yǔ)句在SQLServe里測(cè)試是通過有記錄返回,用PHP的MSSQL擴(kuò)展查詢就是報(bào)錯(cuò) 2012-10-10
-
PHP has encountered an Access Violation at 7C94BD02解決方法
PHP has encountered an Access Violation at 7C94BD02解決方法 2009-08-08
-
PHP批量獲取網(wǎng)頁(yè)中所有固定種子鏈接的方法
這篇文章主要介紹了PHP批量獲取網(wǎng)頁(yè)中所有固定種子鏈接的方法,涉及php字符串與文件操作的相關(guān)技巧,需要的朋友可以參考下 2016-11-11
最新評(píng)論
本文實(shí)例講述了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
聲明的變量來自于生成閉包實(shí)例時(shí)所在作用域內(nèi)的同名變量,而不是來自于運(yùn)行閉包時(shí)所在作用域內(nèi)的同名變量。
而閉包的函數(shù)參數(shù)則是和正常的函數(shù)參數(shù)一樣來自于運(yùn)行時(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
(相當(dāng)于類定義)在編譯期完成,但在運(yùn)行期生成閉包實(shí)例(相當(dāng)于類實(shí)例)時(shí)會(huì)為不同實(shí)例綁定不同的use
靜態(tài)變量(在DECLARE_LAMBDA_FUNCTION中完成)。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
The specified CGI application misbehaved by not returning a
The specified CGI application misbehaved by not returning a complete set of HTTP headers2011-03-03php使用array_search函數(shù)實(shí)現(xiàn)數(shù)組查找的方法
這篇文章主要介紹了php使用array_search函數(shù)實(shí)現(xiàn)數(shù)組查找的方法,涉及php數(shù)組查找的相關(guān)技巧,需要的朋友可以參考下2015-06-06因str_replace導(dǎo)致的注入問題總結(jié)
這篇文章主要給大家介紹了關(guān)于因str_replace導(dǎo)致的注入問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08php mssql擴(kuò)展SQL查詢中文字段名解決方法
問題就出現(xiàn)在SQLServer中表的字段名是中文,寫好的查詢語(yǔ)句在SQLServe里測(cè)試是通過有記錄返回,用PHP的MSSQL擴(kuò)展查詢就是報(bào)錯(cuò)2012-10-10PHP has encountered an Access Violation at 7C94BD02解決方法
PHP has encountered an Access Violation at 7C94BD02解決方法2009-08-08PHP批量獲取網(wǎng)頁(yè)中所有固定種子鏈接的方法
這篇文章主要介紹了PHP批量獲取網(wǎng)頁(yè)中所有固定種子鏈接的方法,涉及php字符串與文件操作的相關(guān)技巧,需要的朋友可以參考下2016-11-11