搜索
首页后端开发php教程深入懂得PHP内核(五)函数的内部结构

深入理解PHP内核(五)函数的内部结构

php的函数包括用户定义的函数、内部函数(print_r count...)、匿名函数、变量函数($func = 'print_r'; $func(array('a','b'));)

PHP内核源码中将函数分为以下类型

<span style="color: #0000ff;">#define</span> ZEND_INTERNAL_FUNCTION              1<span style="color: #0000ff;">#define</span> ZEND_USER_FUNCTION                  2  <span style="color: #0000ff;">#define</span> ZEND_OVERLOADED_FUNCTION            3<span style="color: #0000ff;">#define</span> ZEND_EVAL_CODE                      4<span style="color: #0000ff;">#define</span> ZEND_OVERLOADED_FUNCTION_TEMPORARY  5

一、用户函数(ZEND_USER_FUNCTION)

  函数不一定显式的有返回值,在PHP的实现中即使没有显式的返回,PHP内核也会帮我们返回NULL。

  ZEND在执行过程中,会将运行时信息存储于_zend_execute_data中:

<span style="color: #0000ff;">struct</span><span style="color: #000000;"> _zend_execute_data {    </span><span style="color: #008000;">//</span><span style="color: #008000;">...省略部分代码</span><span style="color: #000000;">    zend_function_state function_state;    zend_function </span>*fbc; <span style="color: #008000;">/*</span><span style="color: #008000;"> Function Being Called </span><span style="color: #008000;">*/</span>    <span style="color: #008000;">//</span><span style="color: #008000;">...省略部分代码</span>};

  在程序初始化的过程中,function_state也会进行初始化,function_state由两个部分组成:

typedef <span style="color: #0000ff;">struct</span><span style="color: #000000;"> _zend_function_state {    zend_function </span>*<span style="color: #000000;">function;    </span><span style="color: #0000ff;">void</span> **<span style="color: #000000;">arguments;} zend_function_state;</span>

  *arguments是一个指向函数参数的指针,而函数体本事存储于*function中,*function是一个zend_function结构体,它最终存储了用户自定义函数的一切信息,具体结构如下:

<span style="color: #000000;">typedef union _zend_function {    zend_uchar type;    </span><span style="color: #008000;">/*</span><span style="color: #008000;"> MUST be the first element of this struct! </span><span style="color: #008000;">*/</span>     <span style="color: #0000ff;">struct</span><span style="color: #000000;"> {        zend_uchar type;  </span><span style="color: #008000;">/*</span><span style="color: #008000;"> never used </span><span style="color: #008000;">*/</span>        <span style="color: #0000ff;">char</span> *function_name;    <span style="color: #008000;">//</span><span style="color: #008000;">函数名称</span>        zend_class_entry *scope; <span style="color: #008000;">//</span><span style="color: #008000;">函数所在的类作用域</span>        zend_uint fn_flags;     <span style="color: #008000;">//</span><span style="color: #008000;">函数类型,如用户自定义则为 #define </span>ZEND_USER_FUNCTION <span style="color: #800080;">2</span><span style="color: #000000;">          union _zend_function </span>*prototype; <span style="color: #008000;">//</span><span style="color: #008000;">函数原型</span>        zend_uint num_args;     <span style="color: #008000;">//</span><span style="color: #008000;">参数数目</span>        zend_uint required_num_args; <span style="color: #008000;">//</span><span style="color: #008000;">需要的参数数目</span>        zend_arg_info *arg_info;  <span style="color: #008000;">//</span><span style="color: #008000;">参数信息指针</span><span style="color: #000000;">        zend_bool pass_rest_by_reference;        unsigned </span><span style="color: #0000ff;">char</span> return_reference;  <span style="color: #008000;">//</span><span style="color: #008000;">返回值</span><span style="color: #000000;">    } common;     zend_op_array op_array;   </span><span style="color: #008000;">//</span><span style="color: #008000;">函数中的操作</span><span style="color: #000000;">?    zend_internal_function internal_function;  } zend_function;</span>

  zend_function的结构体中的op_array存储了该函数中的所有操作,当函数被调用时,ZEND就会将这个op_array中的opline一条条顺序执行,并将最后的结果返回。函数的定义和执行是分开的,一个函数可以作为一个独立的运行单元存在。

二、内部函数(ZEND_INTERNAL_FUNCTION)

  ZEND_INTERNAL_FUNCTION函数是由扩展或者Zend/PHP内核提供的,用c/c++编写,可以直接执行的函数,以下为内部函数的结构

typedef <span style="color: #0000ff;">struct</span><span style="color: #000000;"> _zend_internal_function {    </span><span style="color: #008000;">/*</span><span style="color: #008000;"> Common elements </span><span style="color: #008000;">*/</span><span style="color: #000000;">    zend_uchar type;    </span><span style="color: #0000ff;">char</span> *<span style="color: #000000;"> function_name;    zend_class_entry </span>*<span style="color: #000000;">scope;    zend_uint fn_flags;    union _zend_function </span>*<span style="color: #000000;">prototype;    zend_uint num_args;    zend_uint required_num_args;    zend_arg_info </span>*<span style="color: #000000;">arg_info;    zend_bool pass_rest_by_reference;    unsigned </span><span style="color: #0000ff;">char</span><span style="color: #000000;"> return_reference;    </span><span style="color: #008000;">/*</span><span style="color: #008000;"> END of common elements </span><span style="color: #008000;">*/</span>     <span style="color: #0000ff;">void</span> (*<span style="color: #000000;">handler)(INTERNAL_FUNCTION_PARAMETERS);    </span><span style="color: #0000ff;">struct</span> _zend_module_entry *<span style="color: #000000;">module;} zend_internal_function;</span>

  在模块初始化的时候,ZE会遍历每个载入的扩展模块,然后将模块中function_entry中指明的每一个函数(module->functions),创建一个zend_internal_function结构,并将其type设置为ZEND_INTERNAL_FUNCTION,将这个结构填入全局的函数表(HashTable结构);函数设置及注册过程见Zend/zene_API.c文件中的zend_register_function函数,这个函数除了处理函数页也处理类的方法,包括那些魔术方法。

  内部函数的结构与用户自定义函数结构基本类似,有一些不同:

  •   调用方法,handler字段,如果是ZEND_INTERNAL_FUNCTION,那么ZEND就会调用zend_execute_internal,通过zend_internal_function.handler来执行这个函数。而用户自定义函数需要生成中间代码,然后通过中间代码映射到相对就把方法调用。
  • 内置函数在结构中多了一个module字段,表示属于哪个模块。不同的扩展模块不同
  • type字段,在用户自定义函数中,type字段几乎无用,而内置函数中的type字段作为几种内部函数的区分。

三、变量函数

  如果一个变量名后边有圆括号,php将寻找与变量的值同名的函数,并且尝试执行。

  变量函数$func

$func = <span style="color: #800000;">'</span><span style="color: #800000;">print_r</span><span style="color: #800000;">'</span><span style="color: #000000;">;$func(</span><span style="color: #800000;">'</span><span style="color: #800000;">i am print_r function.</span><span style="color: #800000;">'</span>);

  编译后中间代码

function name:  (<span style="color: #0000ff;">null</span><span style="color: #000000;">)number of ops:  </span><span style="color: #800080;">9</span><span style="color: #000000;">compiled vars:  </span>!<span style="color: #800080;">0</span> =<span style="color: #000000;"> $funcline     # </span>*  op                           fetch          ext  <span style="color: #0000ff;">return</span><span style="color: #000000;"> operands</span>--------------------------------------------------------------------------------   <span style="color: #800080;">2</span>     <span style="color: #800080;">0</span>  ><span style="color: #000000;">   EXT_STMT         </span><span style="color: #800080;">1</span>      ASSIGN                                                   !<span style="color: #800080;">0</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">print_r</span><span style="color: #800000;">'</span>   <span style="color: #800080;">3</span>     <span style="color: #800080;">2</span><span style="color: #000000;">      EXT_STMT         </span><span style="color: #800080;">3</span>      INIT_FCALL_BY_NAME                                       !<span style="color: #800080;">0</span>         <span style="color: #800080;">4</span><span style="color: #000000;">      EXT_FCALL_BEGIN         </span><span style="color: #800080;">5</span><span style="color: #000000;">      SEND_VAL                                                 </span><span style="color: #800000;">'</span><span style="color: #800000;">i+am+print_r+function.</span><span style="color: #800000;">'</span>         <span style="color: #800080;">6</span>      DO_FCALL_BY_NAME                              <span style="color: #800080;">1</span>         <span style="color: #800080;">7</span><span style="color: #000000;">      EXT_FCALL_END         </span><span style="color: #800080;">8</span>    > RETURN                                  1

  内部函数

print_r(<span style="color: #800000;">'</span><span style="color: #800000;">i am print_r function.</span><span style="color: #800000;">'</span>);

  编译后中间代码

function name:  (<span style="color: #0000ff;">null</span><span style="color: #000000;">)number of ops:  </span><span style="color: #800080;">6</span><span style="color: #000000;">compiled vars:  noneline     # </span>*  op                           fetch          ext  <span style="color: #0000ff;">return</span><span style="color: #000000;">  operands</span>---------------------------------------------------------------------------------   <span style="color: #800080;">2</span>     <span style="color: #800080;">0</span>  ><span style="color: #000000;">   EXT_STMT         </span><span style="color: #800080;">1</span><span style="color: #000000;">      EXT_FCALL_BEGIN         </span><span style="color: #800080;">2</span><span style="color: #000000;">      SEND_VAL                                                 </span><span style="color: #800000;">'</span><span style="color: #800000;">i+am+print_r+function.</span><span style="color: #800000;">'</span>         <span style="color: #800080;">3</span>      DO_FCALL                                      <span style="color: #800080;">1</span>          <span style="color: #800000;">'</span><span style="color: #800000;">print_r</span><span style="color: #800000;">'</span>         <span style="color: #800080;">4</span><span style="color: #000000;">      EXT_FCALL_END         </span><span style="color: #800080;">5</span>    > RETURN                                                   <span style="color: #800080;">1</span>

  对比发现,二者在调用中间代码上存在一些区别,变量函数是DO_FCALL_BY_NAME,而内部函数是DO_FCALL。这在语法解析时就已经决定了,见Zend/zend_complie.c文件的zend_do_end_function_call函数中部分代码:

<span style="color: #0000ff;">if</span> (!is_method && !is_dynamic_fcall && function_name->op_type==<span style="color: #000000;">IS_CONST) {        opline</span>->opcode =<span style="color: #000000;"> ZEND_DO_FCALL;        opline</span>->op1 = *<span style="color: #000000;">function_name;        ZVAL_LONG(</span>&opline-><span style="color: #000000;">op2.u.constant, zend_hash_func(Z_STRVAL(function_name</span>->u.constant), Z_STRLEN(function_name->u.constant) + <span style="color: #800080;">1</span><span style="color: #000000;">));    } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {        opline</span>->opcode =<span style="color: #000000;"> ZEND_DO_FCALL_BY_NAME;        SET_UNUSED(opline</span>-><span style="color: #000000;">op1);    }</span>

  如果不是方法,并且不是动态调用,并且函数名为字符串变量,则其生成的中间代码为ZEND_DO_FCALL。其他情况则为ZEND_DO_FCALL_BY_NAME。另外将变量函数作为回调函数,其处理过程在Zend/zend_complie.c文件的zend_do_pass_param函数中,最终会体现在中间代码执行过程中的ZEND_SEND_VAL_SPEC_CONST_HADNLER等函数中。

 

四、匿名函数

  匿名函数是一类不需要指定表示符,而又可以被调用的函数或子例程,匿名函数可以方便的作为参数传递给其他函数。

  

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP电子邮件:分步发送指南PHP电子邮件:分步发送指南May 09, 2025 am 12:14 AM

phpisusedforsendendemailsduetoitsignegrationwithservermailservicesand andexternalsmtpproviders,自动化notifications andMarketingCampaigns.1)设置设置yourphpenvironcormentswironmentswithaweberswithawebserverserverserverandphp,确保themailfunctionisenabled.2)useabasicscruct

如何通过PHP发送电子邮件:示例和代码如何通过PHP发送电子邮件:示例和代码May 09, 2025 am 12:13 AM

发送电子邮件的最佳方法是使用PHPMailer库。1)使用mail()函数简单但不可靠,可能导致邮件进入垃圾邮件或无法送达。2)PHPMailer提供更好的控制和可靠性,支持HTML邮件、附件和SMTP认证。3)确保正确配置SMTP设置并使用加密(如STARTTLS或SSL/TLS)以增强安全性。4)对于大量邮件,考虑使用邮件队列系统来优化性能。

高级PHP电子邮件:自定义标题和功能高级PHP电子邮件:自定义标题和功能May 09, 2025 am 12:13 AM

CustomHeadersheadersandAdvancedFeaturesInphpeMailenHanceFunctionalityAndreliability.1)CustomHeadersheadersheadersaddmetadatatatatataatafortrackingandCategorization.2)htmlemailsallowformattingandttinganditive.3)attachmentscanmentscanmentscanbesmentscanbestmentscanbesentscanbesentingslibrarieslibrarieslibrariesliblarikelikephpmailer.4)smtppapapairatienticationaltication enterticationallimpr

使用PHP和SMTP发送电子邮件的指南使用PHP和SMTP发送电子邮件的指南May 09, 2025 am 12:06 AM

使用PHP和SMTP发送邮件可以通过PHPMailer库实现。1)安装并配置PHPMailer,2)设置SMTP服务器细节,3)定义邮件内容,4)发送邮件并处理错误。使用此方法可以确保邮件的可靠性和安全性。

使用PHP发送电子邮件的最佳方法是什么?使用PHP发送电子邮件的最佳方法是什么?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

PHP中依赖注入的最佳实践PHP中依赖注入的最佳实践May 08, 2025 am 12:21 AM

使用依赖注入(DI)的原因是它促进了代码的松耦合、可测试性和可维护性。1)使用构造函数注入依赖,2)避免使用服务定位器,3)利用依赖注入容器管理依赖,4)通过注入依赖提高测试性,5)避免过度注入依赖,6)考虑DI对性能的影响。

PHP性能调整技巧和技巧PHP性能调整技巧和技巧May 08, 2025 am 12:20 AM

phperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovesponsemetimes.2)优化

PHP电子邮件安全性:发送电子邮件的最佳实践PHP电子邮件安全性:发送电子邮件的最佳实践May 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。