PHP extension development (2)

Source: Internet
Author: User
PHP extension development (2)
> We learned how to quickly scale a PHP hello world! Next we will learn how to pass parameters ### necessary knowledge point 1. variable storage structure (php 5.6 src/ZEND/zend. h) typedef union _ zvalue_value {long lval; // long value double dval; // double value struct {char * val; int len ;}str; HashTable * ht; // zend_object_value obj; zend_ast * ast;} zvalue_value; struct _ zval_struct {// Variable information zvalue_value value; // value zend_uint refcount \__ gc; zend_uchar type; // active type zend_uchar is_ref \__ gc ;}; The final form of a [this is the PHP variable] typedef struct _ zval_struct zval; its actual value is stored in zval. value determines whether to reference is_ref _ gc determines the number of times of reference refcount gc [write-time replication feature of PHP] type of the judgment value ### source code analysis we open zend_API.c and we can see a lot about variable processing function. For example, ZEND_API int zend_get_parameters (int ht, int param_count ,...) ZEND_API int _ zend_get_parameters_array (int ht, int param_count, zval ** argument_array TSRMLS_DC) ZEND_API int zend_get_parameters_ex (int param_count ,...) ZEND_API int _ zend_get_parameters_array_ex (int param_count, zval *** argument_array TSRMLS_DC) ZEND_API int zend_parse_parameters ...... let's refer to the original code. Open the file phpsrc/ext/standard/var. c. let's look at how PHP implements the var_dump method. "87 PHPAPI void php_var_dump (zval ** struc, int level TSRMLS_DC)/* {*/88 {89 HashTable * myht; 90 const char * class_name; 91 zend_uint class_name_len; 92 int (* php_element_dump_func) (zval ** TSRMLS_DC, int, va_list, zend_hash_key *); 93 int is_temp; 94 95 if (level> 1) {96 php_printf ("% * c", level-1, ''); 97} 98 99 switch (Z_TYPE_PP (struc)) {100 case IS_BOOL: 101 php_printf ("% sbool (% s) \ n", COMMON, Z_LVAL_PP (struc )? "True": "false"); 102 break ;......} 173 line PHP_FUNCTION (var_dump) 173 {174 zval *** args; 175 int argc; 176 int I; 177 178 if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "+ ", & args, & argc) = FAILURE) {179 return; 180} 181 182 for (I = 0; I <argc; I ++) {183 php_var_dump (args [I], 1 TSRMLS_CC); 184} 185 efree (args); 186} "it can be found that, as described in the previous section, PHP_FUNCTION (function name) is defined first) {implementation}. what should we do if there are no parameters? Here we will use the method we mentioned at the beginning. The first line is ** zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "+", & args, & argc) ** It is obvious that the number of parameters and parameters are obtained and the implementation body of the function is called here. the idea of how to call the function in PHP extension development is very clear. ### In practice, we still follow yesterday's example. today we write A + B method. First, we define A method "PHP_FUNCTION (example_add) {zval *** args; int argc; int I; if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "+", & args, & argc) = FAILURE) {return ;} // add the operation efree (args);} "add the + B function printf (" % d \ n ", args [0] + args [1]); similarly, we add it to functions "static const zend_function_entry t2_functions [] = {// add the method definition PHP_FE (example_add, arg_info) here) {NULL, NULL, NULL} "re-compile and install, we can run"
 "Output 8

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.