PHP扩展开发 函数返回值未使用后报错的实现原理
在看PHP扩展开发时,看到一处关于PHP函数返回值的问题。
当定义了一个函数,而这个函数拥有返回值,在用户使用时,并没有使用返回值,zend引擎可以实现报错,这是如何实现的,怎么个原理?
<code>ZEND_FUNCTION(sample_array_range) { if (return_value_used) { int i; //把返回值初始化成一个PHP语言中的数组 array_init(return_value); for(i = 0; i </code>
在这个函数定义里面,感觉是先接收
return_value_used
参数,但是当函数运行完成之前,系统应该并不知道该函数返回值是否被使用到了吧!想不通他的实现原理。
求大牛,说说其中的实现原理!
回复内容:
在看PHP扩展开发时,看到一处关于PHP函数返回值的问题。
当定义了一个函数,而这个函数拥有返回值,在用户使用时,并没有使用返回值,zend引擎可以实现报错,这是如何实现的,怎么个原理?
<code>ZEND_FUNCTION(sample_array_range) { if (return_value_used) { int i; //把返回值初始化成一个PHP语言中的数组 array_init(return_value); for(i = 0; i </code>
在这个函数定义里面,感觉是先接收
return_value_used
参数,但是当函数运行完成之前,系统应该并不知道该函数返回值是否被使用到了吧!想不通他的实现原理。
求大牛,说说其中的实现原理!
实现原理其实很简单,在php代码被逐句解释之前,zend引擎实际上做了很多事情,比如说read、parser、compile、cache等等。
这里的return_value_used实际上是在compile阶段产生的,代码变成语法树之后,每个opline都会有一个_znode的结构体,用于保存当前节点的信息,定义如下:
<code>c</code><code>typedef struct _znode { int op_type; union { zval constant; zend_uint var; zend_uint opline_num; /* Needs to be signed */ zend_op_array *op_array; zend_op *jmp_addr; struct { zend_uint var; /* dummy */ zend_uint type; } EA; } u; } znode; </code>
这里内连了一个结构体EA,其中的type就保存了是否被接受的信息。
在compile阶段,他会凭借语法树来动态改变相应的值,比如$a = abc();和abc();和add(abc());都会执行不同的compile 函数,在某些情况下,EA.type会被标识为EXT_TYPE_UNUSED,如果有语法错误,它也能在执行之前就知道。
这里有zend的一份compile.c代码,你可以看一下它里面的各种函数,是针对不同的操作符进行不同处理的。
http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-4...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
