Implementation principle of an error after PHP Extension function return value is not used

Source: Internet
Author: User
When looking at PHP extension development, you can see a question about the PHP function return value. When a function is defined, and this function has a return value, it does not use the return value in user use. The zend engine can implement error reporting. How is this implemented? {Code...} in this function... when looking at PHP extension development, I saw a question about the PHP function return value.

When a function is defined, and this function has a return value, it does not use the return value in user use. The zend engine can implement error reporting. How is this implemented?

ZEND_FUNCTION (sample_array_range) {if (return_value_used) {int I; // initialize the returned value into an array array_init (return_value) in the PHP language; for (I = 0; I <1000; I ++) {// Add new elements to retrun_value continuously. The value is I add_next_index_long (return_value, I);} return ;} else {// throw an E_NOTICE-Level Error php_error_docref (NULL TSRMLS_CC, E_NOTICE, "I will know that you didn't use my work! "); RETURN_NULL ();}}

In this function definition, it is assumed thatreturn_value_usedParameter, but before the function is completed, the system should not know whether the function return value is used! I cannot figure out how it works.

Let's talk about the implementation principles!

Reply content:

When looking at PHP extension development, you can see a question about the PHP function return value.

When a function is defined, and this function has a return value, it does not use the return value in user use. The zend engine can implement error reporting. How is this implemented?

ZEND_FUNCTION (sample_array_range) {if (return_value_used) {int I; // initialize the returned value into an array array_init (return_value) in the PHP language; for (I = 0; I <1000; I ++) {// Add new elements to retrun_value continuously. The value is I add_next_index_long (return_value, I);} return ;} else {// throw an E_NOTICE-Level Error php_error_docref (NULL TSRMLS_CC, E_NOTICE, "I will know that you didn't use my work! "); RETURN_NULL ();}}

In this function definition, it is assumed thatreturn_value_usedParameter, but before the function is completed, the system should not know whether the function return value is used! I cannot figure out how it works.

Let's talk about the implementation principles!

The implementation principle is actually very simple. Before php code is explained one by one, the zend engine has actually done many things, such as read, parser, compile, and cache.

Here, the return_value_used is actually generated in the compile phase. After the code is changed to the syntax tree, each opline will have a _ znode struct to save the information of the current node. The definition is as follows:

ctypedef 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;

A struct EA is connected here, And the type stores the accepted information.

In the compile stage, he will dynamically change the corresponding values with the syntax tree, such as $ a = abc (); and add (abc ()); different compile functions are executed. In some cases, EA. type will be identified as EXT_TYPE_UNUSED. If there is a syntax error, it can be known before execution.

Here is a compile. c code of zend. You can take a look at the various functions in it, which are processed differently for different operators.

Http://www.opensource.apple.com/source/apache_mod_php/apache_mod_php-4...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.