- 相關(guān)推薦
php內(nèi)核分析之zend-compile
學(xué)習(xí)PHP的時(shí)候也許你會(huì)用到關(guān)于PHP內(nèi)核方面的知識(shí),以下是百分網(wǎng)小編精心為大家整理的php內(nèi)核分析之zend_compile,希望對(duì)大家有所幫助!更多內(nèi)容請(qǐng)關(guān)注應(yīng)屆畢業(yè)生網(wǎng)!
這里閱讀的php版本為PHP-7.1.0 RC3,閱讀代碼的平臺(tái)為linux
回到之前看的zend_eval_stringl
ZEND_API int zend_eval_stringl(char *str, size_t str_len, zval *retval_ptr, char *string_name) /* {{{ */
{
...
new_op_array = zend_compile_string(&pv, string_name); // 這個(gè)是把php代碼編譯成為opcode的過程
...
zend_execute(new_op_array, &local_retval); // 這個(gè)是具體的執(zhí)行過程,執(zhí)行opcode,把結(jié)果存儲(chǔ)到local_retval中
...
retval = SUCCESS;
return retval;
}
這里的zend_execute執(zhí)行了兩步,第一步是把php編譯解析成為opcode的過程,我們就先看這個(gè)。
zend_compile_string
zend_compile_string函數(shù)追下去可以追到compile_string
// 將一個(gè)字符串解析成為op_array
zend_op_array *compile_string(zval *source_string, char *filename)
{
zend_lex_state original_lex_state;
zend_op_array *op_array = NULL;
zval tmp;
// 如果傳進(jìn)來要解析的字符為空,則返回null
if (Z_STRLEN_P(source_string)==0) {
return NULL;
}
ZVAL_DUP(&tmp, source_string); // 復(fù)制source_string到zval中
convert_to_string(&tmp); // 如果不是字符類型就轉(zhuǎn)換為字符類型
source_string = &tmp;
zend_save_lexical_state(&original_lex_state); // 保存lex上下文
if (zend_prepare_string_for_scanning(source_string, filename) == SUCCESS) { // 做編譯前的準(zhǔn)備
BEGIN(ST_IN_SCRIPTING); // 設(shè)置狀態(tài)為正在編譯
op_array = zend_compile(ZEND_EVAL_CODE); // 進(jìn)行編譯,并把生成結(jié)果放在op_array中
}
zend_restore_lexical_state(&original_lex_state); // 恢復(fù)lex上下文
zval_dtor(&tmp); // 釋放tmp
return op_array;
}
里面最核心的就是zend_compile了。這里的幾個(gè)點(diǎn)可以看看:
1 這么做類型轉(zhuǎn)換,參考convert_to_string。這個(gè)函數(shù)就是把任意類型的值轉(zhuǎn)換為zval的string類型。
2 zval_dtor,這個(gè)函數(shù)是能把任意的zval變量做回收。非常好用。
3 ZVAL_DUP,是進(jìn)行復(fù)制,它和ZVAL_COPY的區(qū)別在于是否增加gc的引用計(jì)數(shù)。
下面又找到關(guān)鍵點(diǎn):zend_compi
【php內(nèi)核分析之zend-compile】相關(guān)文章:
php內(nèi)核分析之zval09-07
php內(nèi)核分析之opcode08-04
php內(nèi)核分析之全局變量09-03
php內(nèi)核分析之do-cli05-30
php內(nèi)核分析之sapi-module-struct10-17
php內(nèi)核分析之ZTS和zend-try07-18
php學(xué)習(xí)之php配置07-15