這篇文章主要介紹了PHP之OpCode原理,較為詳細的分析了php程式的相關編譯機制與運行原理,需要的朋友可以參考下
OpCode是一種PHP腳本編譯後的中間語言,就像Java的ByteCode,或.NET的MSL。此文主要基於《 Understanding OPcode》和網絡,根據個人的理解和修改,特記錄下來:
#PHP代碼:
##
<?php echo "Hello World"; $a = 1 + 1; echo $a; ?>
PHP執行這段程式碼會經過以下4個步驟:1. Scanning (Lexing) ,將PHP程式碼轉換為語言片段(Tokens)
2. Parsing , 將Tokens轉換成簡單而有意義的表達式
3. Compilation , 將表達式編譯成Opocdes
4. Execution , 順次執行Opcodes,每次一條,從而實現PHP腳本的功能。
Array ( [0] => Array ( [0] => 367 [1] => <?php [2] => 1 ) [1] => Array ( [0] => 370 [1] => [2] => 2 ) [2] => Array ( [0] => 316 [1] => echo [2] => 2 ) [3] => Array ( [0] => 370 [1] => [2] => 2 ) [4] => Array ( [0] => 315 [1] => "Hello World" [2] => 2 ) [5] => ; [6] => Array ( [0] => 370 [1] => [2] => 2 ) [7] => Array ( [0] => 309 [1] => $a [2] => 3 ) [8] => Array ( [0] => 370 [1] => [2] => 3 ) [9] => = [10] => Array ( [0] => 370 [1] => [2] => 3 ) [11] => Array ( [0] => 305 [1] => 1 [2] => 3 ) [12] => Array ( [0] => 370 [1] => [2] => 3 ) [13] => + [14] => Array ( [0] => 370 [1] => [2] => 3 ) [15] => Array ( [0] => 305 [1] => 1 [2] => 3 ) [16] => ; [17] => Array ( [0] => 370 [1] => [2] => 3 ) [18] => Array ( [0] => 316 [1] => echo [2] => 4 ) [19] => Array ( [0] => 370 [1] => [2] => 4 ) [20] => Array ( [0] => 309 [1] => $a [2] => 4 ) [21] => ; [22] => Array ( [0] => 370 [1] => [2] => 4 ) [23] => Array ( [0] => 369 [1] => ?> [2] => 5 ) )
2. add two numbers together
3. store the result of the prior expression to a variable
4. echo a variable
2. 結果存放Opcode結果
3. 運算元1給Opcode的運算元
4. 運算元2
5. 擴充值1個整形用來區別被重載的運算子
[root@localhost html]# /usr/local/php/bin/php -dvld.active=1 hello.php Branch analysis from position: 0 Return found filename: /var/www/html/hello.php function name: (null) number of ops: 6 compiled vars: !0 = $a line # op fetch ext return operands ------------------------------------------------------------------------------- 2 0 ECHO 'Hello+world' 3 1 ADD ~0 1, 1 2 ASSIGN !0, ~0 4 3 ECHO !0 6 4 RETURN 1 5* ZEND_HANDLE_EXCEPTION Hello world2
以上是PHP中OpCode原理詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!