Heim  >  Artikel  >  php教程  >  深入理解PHP原理之执行周期分析,深入理解php

深入理解PHP原理之执行周期分析,深入理解php

WBOY
WBOYOriginal
2016-06-13 08:38:22817Durchsuche

深入理解PHP原理之执行周期分析,深入理解php

本文讲述了PHP原理之执行周期。分享给大家供大家参考,具体如下:

PHP的执行周期,从最初我们编写的PHP脚本->到最后脚本被执行->得到执行结果,这个过程,其实可以分为如下几个阶段:

首先,Zend Engine(ZE),调用词法分析 器(Lex生成的,源文件在 Zend/zend_language_sanner.l), 将我们要执行的PHP源文件,去掉空格 ,注释,分割成一个一个的token。

然后,ZE会将得到的token forward给语法分析 器(yacc生成, 源文件在 Zend/zend_language_parser.y),生成一个一个的opcode,opcode一般会以op array的形式存在,它是PHP执行的中间语言。

最后,ZE调用zend_executor来执行op array ,输出结果。(也就是将源文件转换成机器语言,然后在虚拟机上运行它。)

ZE是一个虚拟机,正是由于它的存在,所以才能使得我们写PHP脚本,完全不需要考虑所在的操作系统类型是什么,这才是PHP的可移植性的原因。ZE是一个CISC(复杂指令处理器),它支持150条指令(具体指令在 Zend/zend_vm_opcodes.h),包括从最简单的ZEND_ECHO(echo)到复杂的 ZEND_INCLUDE_OR_EVAL(include,require),所有我们编写的PHP都会最终被处理为这150条指令(op code)的序列,从而最终被执行

PHP是一个脚本语言,也就是说,用户编写的PHP代码最终都是会被PHP解释器解释执行,所有编写的PHP代码,都会被翻译成PHP的虚拟机ZE的虚拟指令(OPCODES)来执行。

那我们的PHP脚本,最终被“翻译"成什么样的呢? 也就是说,op code长的什么样子呢? Opcode是一种PHP脚本编译后的中间语言

在PECL中已经有这样的模块,利用由 Derick Rethans开发的VLD (Vulcan Logic Dissassembler)模块。你只要下载这个模块,并把他载入PHP中,就可以通过简单的设置,来得到脚本翻译的结果了。

VLD模块的安装以及应用:

[root@localhost software]# tar zxvf vld-0.9.1.tgz.gz
[root@localhost vld-0.9.1]# /usr/local/php/bin/phpize
[root@localhost vld-0.9.1]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@localhost vld-0.9.1]# make install //不需要make

编辑php.ini文件并激活vld扩展。

实例:

创建一个文件,如:hello.php

<&#63;php
  echo 'hello, world.';
&#63;>

执行:

[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: 3
compiled vars: none
line   # op              fetch     ext return operands
-------------------------------------------------------------------------------
  2   0 ECHO                           'hello%2C+world.'
  4   1 RETURN                          1
     2* ZEND_HANDLE_EXCEPTION
hello, world.

看另一个:

[root@localhost html]# vi vld.php
<&#63;php
  $i = "This is a string";
  //I am comments
  echo $i. ' that has been echoed on screen';
&#63;>

执行:

[root@localhost html]# /usr/local/php/bin/php -dvld.active=1 vld.php
Branch analysis from position: 0
Return found
filename:    /var/www/html/vld.php
function name: (null)
number of ops: 5
compiled vars: !0 = $i
line   # op              fetch     ext return operands
-------------------------------------------------------------------------------
  3   0 ASSIGN                          !0, 'This+is+a+string'
  7   1 CONCAT                      ~1  !0, '+that+has+been+echoed+on+screen'
     2 ECHO                           ~1
 10  3 RETURN                          1
     4* ZEND_HANDLE_EXCEPTION
This is a string that has been echoed on screen

注:ZEND_HANDLE_EXCEPTION 就是 Zend/zend_vm_opcodes.h 中第149条指令

compiled vars:  !0 = $i  此处是获取变量名"i"的变量于!0(*zval)。
#0 将字符串"this+is+a+string"赋值(ASSIGN)给!0
#1 字符串连接
#2 显示

这些中间代码会被Zend VM(Zend虚拟机)直接执行。真正负责执行的函数是:zend_execute(zend_execute.h)。

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数学运算技巧总结》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn