我們知道PHP是一門解釋型語言,用它們編寫的動態內容都需要依賴相應的解譯器程式來運行,解譯器程式需要對輸入的指令碼代碼進行分析,便將它們產生可以直接啟動並執行中間代碼,也稱為作業碼(Operate Code,opcode)。
想要查看php程式的opcode,需要安裝php的parsekit擴充。
安裝過程如下:
[root@localhost ~]# wget http://pecl.php.net/get/parsekit-1.3.0.tgz
[root@localhost ~]# tar zxvf parsekit-1.3.0.tgz
[root@localhost ~]# cd parsekit-1.3.0
[root@localhost parsekit-1.3.0]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version: 20041225
Zend Module Api No: 20060613
Zend Extension Api No: 220060519
[root@localhost parsekit-1.3.0]# ./configure -with-php-config=/usr/local/php/bin/php-config
[root@localhost parsekit-1.3.0]# make
[root@localhost parsekit-1.3.0]# make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/
然後編輯php.ini檔案,在裡面加上
extension="parsekit.so"
[root@localhost parsekit-1.3.0]# vim /usr/local/php/etc/php.ini
這樣我們就成功的安裝了parsekit擴充了(註:樓主是在php5.2下安裝的parsekit擴充,在php5.4,php5.5下會提示編譯失敗)。
當然還有一種更簡單的安裝parsekit的方法,那就是直接使用pecl。
在命令列下輸入命令:
[root@localhost parsekit-1.3.0]# /usr/local/php/bin/pecl install parsekit
這樣就可以自動幫你安裝parsekit了。
然後,我們在命令列下輸入:
[root@localhost parsekit-1.3.0]# /usr/local/php/bin/php -r "var_dump(parsekit_compile_string('print 1+1;'));"
這樣就可以查看php代碼的opcode了。
結果如下:
array(20) {
["type"]=>
int(4)
["type_name"]=>
string(14) "ZEND_EVAL_CODE"
["fn_flags"]=>
int(0)
["num_args"]=>
int(0)
["required_num_args"]=>
int(0)
["pass_rest_by_reference"]=>
bool(false)
["uses_this"]=>
bool(false)
["line_start"]=>
int(0)
["line_end"]=>
int(0)
["return_reference"]=>
bool(false)
["refcount"]=>
int(1)
["last"]=>
int(5)
["size"]=>
int(5)
["T"]=>
int(2)
["last_brk_cont"]=>
int(0)
["current_brk_cont"]=>
int(4294967295)
["backpatch_count"]=>
int(0)
["done_pass_two"]=>
bool(true)
["filename"]=>
string(17) "Parsekit Compiler"
["opcodes"]=>
array(5) {
[0]=>
array(8) {
["address"]=>
int(29981148)
["opcode"]=>
int(1)
["opcode_name"]=>
string(8) "ZEND_ADD"
["flags"]=>
int(197378)
["result"]=>
array(3) {
["type"]=>
int(2)
["type_name"]=>
string(10) "IS_TMP_VAR"
["var"]=>
int(0)
}
["op1"]=>
array(3) {
["type"]=>
int(1)
["type_name"]=>
string(8) "IS_CONST"
["constant"]=>
&int(1)
}
["op2"]=>
array(3) {
["type"]=>
int(1)
["type_name"]=>
string(8) "IS_CONST"
["constant"]=>
&int(1)
}
["lineno"]=>
int(1)
}
[1]=>
array(7) {
["address"]=>
int(29981268)
["opcode"]=>
int(41)
["opcode_name"]=>
string(10) "ZEND_PRINT"
["flags"]=>
int(770)
["result"]=>
array(3) {
["type"]=>
int(2)
["type_name"]=>
string(10) "IS_TMP_VAR"
["var"]=>
int(1)
}
["op1"]=>
array(3) {
["type"]=>
int(2)
["type_name"]=>
string(10) "IS_TMP_VAR"
["var"]=>
int(0)
}
["lineno"]=>
int(1)
}
[2]=>
array(7) {
["address"]=>
int(29981388)
["opcode"]=>
int(70)
["opcode_name"]=>
string(9) "ZEND_FREE"
["flags"]=>
int(271104)
["op1"]=>
array(4) {
["type"]=>
int(2)
["type_name"]=>
string(10) "IS_TMP_VAR"
["var"]=>
int(1)
["EA.type"]=>
int(0)
}
["op2"]=>
array(3) {
["type"]=>
int(8)
["type_name"]=>
string(9) "IS_UNUSED"
["opline_num"]=>
string(1) "0"
}
["lineno"]=>
int(1)
}
[3]=>
array(7) {
["address"]=>
int(29981508)
["opcode"]=>
int(62)
["opcode_name"]=>
string(11) "ZEND_RETURN"
["flags"]=>
int(16777984)
["op1"]=>
array(3) {
["type"]=>
int(1)
["type_name"]=>
string(8) "IS_CONST"
["constant"]=>
&NULL
}
["extended_value"]=>
int(0)
["lineno"]=>
int(1)
}
[4]=>
array(5) {
["address"]=>
int(29981628)
["opcode"]=>
int(149)
["opcode_name"]=>
string(21) "ZEND_HANDLE_EXCEPTION"
["flags"]=>
int(0)
["lineno"]=>
int(1)
}
}
}