foreach是PHP中很常用的一個用作數組循環的控制語句。
因為它的方便和易用,自然也就在後端隱藏著很復雜的具體實現方式(對用戶透明)
今天,我們就來一起分析分析,foreach是如何實現數組(對象)的遍歷的。
我們知道PHP是一個腳本語言,也就是說,用戶編寫的PHP代碼最終都是會被PHP解釋器解釋執行,
特別的,對於PHP來說,所有的用戶編寫的PHP代碼,都會被翻譯成PHP的虛擬機ZE的虛擬指令(OPCODES)來執行,不論細節的話,就是說,我們所編寫的任何PHP腳本,都會最終被翻譯成一條條的指令,從而根據指令,由相應的C編寫的函數來執行。
那麼foreach會被翻譯成什麼樣子呢?
foreach($arr as $key => $val){ echo $key . '=>' . $val . "\n"; }
在詞法分析階段,foreach會被識別為一個TOKEN:T_FOREACH,
在語法分析階段,會被規則:
unticked_statement: //沒有被綁定ticks的語句 //有省略 | T_FOREACH '(' variable T_AS { zend_do_foreach_begin(&$1, &$2, &$3, &$4, 1 TSRMLS_CC); } foreach_variable foreach_optional_arg ')' { zend_do_foreach_cont(&$1, &$2, &$4, &$6, &$7 TSRMLS_CC); } foreach_statement { zend_do_foreach_end(&$1, &$4 TSRMLS_CC); } | T_FOREACH '(' expr_without_variable T_AS { zend_do_foreach_begin(&$1, &$2, &$3, &$4, 0 TSRMLS_CC); } variable foreach_optional_arg ')' { zend_check_writable_variable(&$6); zend_do_foreach_cont(&$1, &$2, &$4, &$6, &$7 TSRMLS_CC); } foreach_statement { zend_do_foreach_end(&$1, &$4 TSRMLS_CC); } //有省略 ;
仔細分析這段語法規則,我們可以發現,對於:
foreach($arr as $key => $val){ echo $key . ‘=>' . $val .”\n”; }
會被分析為:
T_FOREACH '(' variable T_AS { zend_do_foreach_begin('foreach', '(', $arr, 'as', 1 TSRMLS_CC); } foreach_variable foreach_optional_arg(T_DOUBLE_ARROW foreach_variable) ')' { zend_do_foreach_cont('foreach', '(', 'as', $key, $val TSRMLS_CC); } foreach_satement {zend_do_foreach_end('foreach', 'as');}
然後,讓我們來看看foreach_statement:
它其實就是一個代碼塊,體現了我們的 echo $key . ‘=>' . $val .”\n”;
T_ECHO expr;
顯然,實現foreach的核心就是如下3個函數:
其中,zend_do_foreach_begin (代碼太長,直接寫偽碼) 主要做了:
1. 記錄當前的opline行數(為以後跳轉而記錄)
2. 對數組進行RESET(講內部指針指向第一個元素)
3. 獲取臨時變量 ($val)
4. 設置獲取變量的OPCODE FE_FETCH,結果存第3步的臨時變量
4. 記錄獲取變量的OPCODES的行數
而對於 zend_do_foreach_cont來說:
1. 根據foreach_variable的u.EA.type來判斷是否引用
2. 根據是否引用來調整zend_do_foreach_begin中生成的FE_FETCH方式
3. 根據zend_do_foreach_begin中記錄的取變量的OPCODES的行數,來初始化循環(主要處理在循環內部的循環:do_begin_loop)
最後zend_do_foreach_end:
1. 根據zend_do_foreach_begin中記錄的行數信息,設置ZEND_JMP OPCODES
2. 根據當前行數,設置循環體下一條opline, 用以跳出循環
3. 結束循環(處理循環內循環:do_end_loop)
4. 清理臨時變量
當然, 在zend_do_foreach_cont 和 zend_do_foreach_end之間 會在語法分析階段被填充foreach_satement的語句代碼。
這樣,就實現了foreach的OPCODES line。
比如對於我們開頭的實例代碼,最終生成的OPCODES是:
filename: /home/huixinchen/foreach.php function name: (null) number of ops: 17 compiled vars: !0 = $arr, !1 = $key, !2 = $val line # op fetch ext return operands ------------------------------------------------------------------------------- 2 0 SEND_VAL 1 1 SEND_VAL 100 2 DO_FCALL 2 'range' 3 ASSIGN !0, $0 3 4 FE_RESET $2 !0, ->14 5 FE_FETCH $3 $2, ->14 6 ZEND_OP_DATA ~5 7 ASSIGN !2, $3 8 ASSIGN !1, ~5 4 9 CONCAT ~7 !1, '-' 10 CONCAT ~8 ~7, !2 11 CONCAT ~9 ~8, '%0A' 12 ECHO ~9 5 13 JMP ->5 14 SWITCH_FREE $2 7 15 RETURN 1 16* ZEND_HANDLE_EXCEPTION
我們注意到FE_FETCH的op2的操作數是14,也就是JMP後一條opline,也就是說,在獲取完最後一個數組元素以後,FE_FETCH失敗的情況下,會跳到第14行opline,從而實現了循環的結束。
而15行opline的op1的操作數是指向了FE_FETCH,也就是無條件跳轉到第5行opline,從而實現了循環。
附錄:
void zend_do_foreach_begin(znode *foreach_token, znode *open_brackets_token, znode *array, znode *as_token, int variable TSRMLS_DC) { zend_op *opline; zend_bool is_variable; zend_bool push_container = 0; zend_op dummy_opline; if (variable) { //是否是匿名數組 if (zend_is_function_or_method_call(array)) { //是否是函數返回值 is_variable = 0; } else { is_variable = 1; } /* 使用括號記錄FE_RESET的opline行數 */ open_brackets_token->u.opline_num = get_next_op_number(CG(active_op_array)); zend_do_end_variable_parse(BP_VAR_W, 0 TSRMLS_CC); //獲取數組/對象和zend_do_begin_variable_parse對應 if (CG(active_op_array)->last > 0 && CG(active_op_array)->opcodes[CG(active_op_array)->last-1].opcode == ZEND_FETCH_OBJ_W) { /* Only lock the container if we are fetching from a real container and not $this */ if (CG(active_op_array)->opcodes[CG(active_op_array)->last-1].op1.op_type == IS_VAR) { CG(active_op_array)->opcodes[CG(active_op_array)->last-1].extended_value |= ZEND_FETCH_ADD_LOCK; push_container = 1; } } } else { is_variable = 0; open_brackets_token->u.opline_num = get_next_op_number(CG(active_op_array)); } foreach_token->u.opline_num = get_next_op_number(CG(active_op_array)); //記錄數組Reset Opline number opline = get_next_op(CG(active_op_array) TSRMLS_CC); //生成Reset數組Opcode opline->opcode = ZEND_FE_RESET; opline->result.op_type = IS_VAR; opline->result.u.var = get_temporary_variable(CG(active_op_array)); opline->op1 = *array; SET_UNUSED(opline->op2); opline->extended_value = is_variable ? ZEND_FE_RESET_VARIABLE : 0; dummy_opline.result = opline->result; if (push_container) { dummy_opline.op1 = CG(active_op_array)->opcodes[CG(active_op_array)->last-2].op1; } else { znode tmp; tmp.op_type = IS_UNUSED; dummy_opline.op1 = tmp; } zend_stack_push(&CG(foreach_copy_stack), (void *) &dummy_opline, sizeof(zend_op)); as_token->u.opline_num = get_next_op_number(CG(active_op_array)); //記錄循環起始點 opline = get_next_op(CG(active_op_array) TSRMLS_CC); opline->opcode = ZEND_FE_FETCH; opline->result.op_type = IS_VAR; opline->result.u.var = get_temporary_variable(CG(active_op_array)); opline->op1 = dummy_opline.result; //被操作數組 opline->extended_value = 0; SET_UNUSED(opline->op2); opline = get_next_op(CG(active_op_array) TSRMLS_CC); opline->opcode = ZEND_OP_DATA; //當使用key的時候附屬操作數,當foreach中不包含key時忽略 SET_UNUSED(opline->op1); SET_UNUSED(opline->op2); SET_UNUSED(opline->result); } void zend_do_foreach_cont(znode *foreach_token, const znode *open_brackets_token, const znode *as_token, znode *value, znode *key TSRMLS_DC) { zend_op *opline; znode dummy, value_node; zend_bool assign_by_ref=0; opline = &CG(active_op_array)->opcodes[as_token->u.opline_num]; //獲取FE_FETCH Opline if (key->op_type != IS_UNUSED) { znode *tmp;//交換key和val tmp = key; key = value; value = tmp; opline->extended_value |= ZEND_FE_FETCH_WITH_KEY; //表明需要同時獲取key和val } if ((key->op_type != IS_UNUSED) && (key->u.EA.type & ZEND_PARSED_REFERENCE_VARIABLE)) { //key不能以引用方式獲取 zend_error(E_COMPILE_ERROR, "Key element cannot be a reference"); } if (value->u.EA.type & ZEND_PARSED_REFERENCE_VARIABLE) { //以引用方式獲取值 assign_by_ref = 1; if (!(opline-1)->extended_value) { //根據FE_FETCH的上一條Opline也就是獲取數組的擴展值來判斷數組是否是匿名數組 zend_error(E_COMPILE_ERROR, "Cannot create references to elements of a temporary array expression"); } opline->extended_value |= ZEND_FE_FETCH_BYREF; //指明按引用取 CG(active_op_array)->opcodes[foreach_token->u.opline_num].extended_value |= ZEND_FE_RESET_REFERENCE; //重置原數組 } else { zend_op *foreach_copy; zend_op *fetch = &CG(active_op_array)->opcodes[foreach_token->u.opline_num]; zend_op *end = &CG(active_op_array)->opcodes[open_brackets_token->u.opline_num]; /* Change "write context" into "read context" */ fetch->extended_value = 0; /* reset ZEND_FE_RESET_VARIABLE */ while (fetch != end) { --fetch; if (fetch->opcode == ZEND_FETCH_DIM_W && fetch->op2.op_type == IS_UNUSED) { zend_error(E_COMPILE_ERROR, "Cannot use [] for reading"); } fetch->opcode -= 3; /* FETCH_W -> FETCH_R */ } /* prevent double SWITCH_FREE */ zend_stack_top(&CG(foreach_copy_stack), (void **) &foreach_copy); foreach_copy->op1.op_type = IS_UNUSED; } value_node = opline->result; if (assign_by_ref) { zend_do_end_variable_parse(value, BP_VAR_W, 0 TSRMLS_CC); //獲取值(引用) zend_do_assign_ref(NULL, value, &value_node TSRMLS_CC);//指明value node的type是IS_VAR } else { zend_do_assign(&dummy, value, &value_node TSRMLS_CC); //獲取copy值 zend_do_free(&dummy TSRMLS_CC); } if (key->op_type != IS_UNUSED) { znode key_node; opline = &CG(active_op_array)->opcodes[as_token->u.opline_num+1]; opline->result.op_type = IS_TMP_VAR; opline->result.u.EA.type = 0; opline->result.u.opline_num = get_temporary_variable(CG(active_op_array)); key_node = opline->result; zend_do_assign(&dummy, key, &key_node TSRMLS_CC); zend_do_free(&dummy TSRMLS_CC); } do_begin_loop(TSRMLS_C); INC_BPC(CG(active_op_array)); } void zend_do_foreach_end(znode *foreach_token, znode *as_token TSRMLS_DC) { zend_op *container_ptr; zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC); //生成JMP opcode opline->opcode = ZEND_JMP; opline->op1.u.opline_num = as_token->u.opline_num; //設置JMP到FE_FETCH opline行 SET_UNUSED(opline->op1); SET_UNUSED(opline->op2); CG(active_op_array)->opcodes[foreach_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array)); //設置跳出循環的opline行 CG(active_op_array)->opcodes[as_token->u.opline_num].op2.u.opline_num = get_next_op_number(CG(active_op_array)); //同上 do_end_loop(as_token->u.opline_num, 1 TSRMLS_CC); //為循環嵌套而設置 zend_stack_top(&CG(foreach_copy_stack), (void **) &container_ptr); generate_free_foreach_copy(container_ptr TSRMLS_CC); zend_stack_del_top(&CG(foreach_copy_stack)); DEC_BPC(CG(active_op_array)); //為PHP interactive模式而設置 }
同時還要注意的是,foreach在使用中是值還是傳引用的問題。
php 中遍歷一個array時可以使用for或foreach,foreach的語法為:foreach ($arr as $k => $v)。遍歷數組,把index賦給$k,數組的值賦給$v,那麼此處的賦值是傳值還是傳引用呢。先看下面的例子:
$arr = array( array('id' => 1, 'name' => 'name1'), array('id' => 2, 'name' => 'name2'), ); foreach ($arr as $obj) { $obj['id'] = $obj['id']; $obj['name'] = $obj['name'] . '-modify'; } print_r($arr); //輸出的結果 Array( [0] => Array ( [id] => 1 [name] => name1 ) [1] => Array( [id] => 2 [name] => name2 ) )
觀察可以發現在foreach循環中對$arr操作並沒有影響到$arr的元素,所以這裡的賦值是傳值而不是傳引用。那如果需要修改$arr中元素的值該怎麼辦呢?可以在變量前面加一個”&”符號,例如:
foreach ($arr as &$obj) { $obj['id'] = $obj['id']; $obj['name'] = $obj['name'] . '-modify'; }
再看另外一個例子,array裡面存放的是object,
$arr = array( (object)(array('id' => 1, 'name' => 'name1')), (object)(array('id' => 2, 'name' => 'name2')), ); foreach ($arr as $obj) { $obj->name = $obj->name . '-modify'; } print_r($arr); //輸出的結果 Array ( [0] => stdClass Object ( [id] => 1 [name] => name1-modify ) [1] => stdClass Object ( [id] => 2 [name] => name2-modify ) )
此時可以看到原始數組中的object對象已經修改了,所以這裡的賦值又是傳引用而不是傳值
綜合上述,得出的結論:如果數組裡面存放的是普通類型的元素就是采用傳值的方式,存放對象類型元素采用的方式為傳地址。