您的位置 首页 php

如何编译php文件

PHP是解析型高级语言,事实上从Zend内核的角度来看PHP就是一个普通的C程序,它有main函数,我们写的PHP代码是这个程序的输入,然后经过内核的处理输出结果,内核将PHP代码"翻译"为C程序可识别的过程就是PHP的编译。

推荐课程:PHP教程。

如何编译php文件

C程序在编译时将一行行代码编译为机器码,每一个操作都认为是一条机器指令,这些指令写入到编译后的二进制程序中,执行的时候将二进制程序load进相应的内存区域(常量区、数据区、代码区)、分配运行栈,然后从代码区起始位置开始执行,这是C程序编译、执行的简单过程。

同样,PHP的编译与普通的C程序类似,只是PHP代码没有编译成机器码,而是解析成了若干条opcode数组,每条opcode就是C里面普通的struct,含义对应C程序的机器指令,执行的过程就是引擎依次执行opcode,比如我们在PHP里定义一个变量:$a = 123;,最终到内核里执行就是malloc一块内存,然后把值写进去。

在zend_compile.h文件中,opcode结构:

struct _zend_op {    const void *handler; //对应执行的C语言function,即每条opcode都有一个C function处理    znode_op op1; //操作数1    znode_op op2; //操作数2    znode_op result; //返回值    uint32_t extended_value;     uint32_t lineno;    zend_uchar opcode;  //opcode指令    zend_uchar op1_type; //操作数1类型    zend_uchar op2_type; //操作数2类型    zend_uchar result_type; //返回值类型};

所以PHP的解析过程任务就是将PHP代码(通过词法分析re2c,语法分析bison)转化为opcode数组,代码里的所有信息都保存在opcode中,然后将opcode数组交给zend引擎执行,opcode就是内核具体执行的命令,比如赋值、加减操作、函数调用等,每一条opcode都对应一个处理handle,这些handler是提前定义好的C函数。

struct _zend_op_array {    //common是普通函数或类成员方法对应的opcodes快速访问时使用的字段    /* Common elements */    zend_uchar type;    zend_uchar arg_flags[3]; /* bitset of arg_info.pass_by_reference */    uint32_t fn_flags;    zend_string *function_name;    zend_class_entry *scope;    zend_function *prototype;    uint32_t num_args;    uint32_t required_num_args;    zend_arg_info *arg_info;    /* END of common elements */    uint32_t *refcount;    uint32_t last;     //opcode指令数组    zend_op *opcodes;    //PHP代码里定义的变量数:op_type为IS_CV的变量,不含IS_TMP_VAR、IS_VAR的    //编译前此值为0,然后发现一个新变量这个值就加1    int last_var;    //临时变量数:op_type为IS_TMP_VAR、IS_VAR的变量    uint32_t T;    //PHP变量名数组    zend_string **vars;//这个数组在ast编译期间配合last_var用来确定各个变量的编号,非常重要的一步操作    int last_live_range;    int last_try_catch;    zend_live_range *live_range;    zend_try_catch_element *try_catch_array;    //静态变量符号表:通过static声明的    /* static variables support */    HashTable *static_variables;    zend_string *filename;    uint32_t line_start;    uint32_t line_end;    zend_string *doc_comment;    uint32_t early_binding; /* the linked list of delayed declarations */    //字面量数量    int last_literal;    //字面量(常量)数组,这些都是在PHP代码定义的一些值    zval *literals;    //运行时缓存数组大小    int  cache_size;    //运行时缓存,主要用于缓存一些znode_op以便于快速获取数据,后面单独介绍这个机制    void **run_time_cache;    void *reserved[ZEND_MAX_RESERVED_RESOURCES];};

opcode指令:即PHP代码具体对应的处理动作,与二进制程序中的代码段对应
字面量存储:PHP代码中定义的一些变量初始值、调用的函数名称、类名称、常量名称等等称之为字面量,这些值用于执行时初始化变量、函数调用等等
变量分配情况:与字面量类似,这里指的是当前opcodes定义了多少变量、临时变量,每个变量都有一个对应的编号,执行初始化按照总的数目一次性分配zval,使用时也完全按照编号索引,而不是根据变量名索引

从PHP代码到opcode是怎么实现的?

最容易想到的方式就是正则匹配,当然过程没有这么简单。PHP编译过程包括词法分析、语法分析,使用re2c、bison完成,旧的PHP版本直接生成了opcode,PHP7新增了抽象语法树(AST),在语法分析阶段生成AST,然后再生成opcode数组

以上就是如何编译php文件的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:如何编译php文件

文章地址:https://www.zhihuclub.com/4716.shtml

关于作者: 智云科技

热门文章

评论已关闭

26条评论

  1. should you need someone to try out any forms of help in this area I would like to put myself forward please, if you can help me it would be a miracle, please help

  2. This may in part be due to the fact that these dogs were intact until they developed mammary tumors as middle aged to older dogs

  3. Combined doxorubicin vinorelbine Navelbine therapy in the treatment of advanced breast cancer review Semin Oncol and Canada, China and India are major flaxseed producers

  4. Гў So we have to have him on the floor before we could really see how weГў re going to reach our full potential PMID 16034487 Review

  5. Cefaclor CECLOR CECLOR CD Cefradroxil DURICEF Cefixime SUPRAX Cefprozil CEFZIL Cephalexin KEFLEX, KEFTAB Macrolides To treat infections caused by bacteria and fungi

  6. There are no well defined effective treatments for the hyperpigmentation seen in MAS Dallas gRSyLFtWQMxoaKj 5 20 2022

  7. 5mg tablets reviews We will promote trials by private capital to initiate the establishment of private banks responsible for their own risks, as well as financial leasing companies and consumer finance companies and other financial institutions

  8. Material and methods Recent publications were critically reviewed All tests were two sided, and a P value of less than 0

  9. Data were analyzed using the BioRad CFX Manager 3 Apart from interests, the most important thing is felodipine for high blood pressure naturally talents

  10. diclofenac stendra 200 mg coupon Citing unidentified people familiar with the matter, the newspaper said that the Canadian company plans to make the cuts through layoffs that will occur in all of its departments, potentially affecting several thousand people Kenton DMPXzIwbDv 6 19 2022

  11. Additionally, Zyflamend enhanced STZ and ADR induced expression of autophagy beclin 1, LC3 I II, ATG5, and ATG7 and apoptosis cleaved caspase 3 markers Fig For cruciferous vegetables, the consumption of broccoli, cabbage, and cauliflower was documented separately as raw or cooked, whereas intakes of other vegetables, including Brussels sprouts, kale, turnip greens, collard greens, and mustard greens, were combined intakes of both raw and cooked

  12. geriforte estrace 0 The meta analysis showed fewer SAEs in the group treated with loop diuretics vs

  13. Because ICSI is used when men have fertility problems, it is believed that the infertility, rather than the treatment itself, is the cause of the higher risk They ate very simply

  14. Therefore, food reduce blood pressure the two kings only used their no longer need most of my blood pressure medication peak power to fight for a few moments

  15. This cycle, in turn, serves to increase the subjective loudness and importance of the perceived sound

网站地图