PHP extension development - Confusion caused by two zend_api during PHP extension development

WBOY
Release: 2016-09-06 08:57:08
Original
954 people have browsed it

My extension code is like this

<code>ZEND_BEGIN_ARG_INFO_EX(helloZvalArg, 0, 0, 1)
    ZEND_ARG_INFO (0, name)
    ZEND_ARG_INFO (0, age)
    ZEND_ARG_INFO (0, zval_args)
ZEND_END_ARG_INFO()



PHP_FUNCTION(debug_zval)
{
    zend_string *name, *age;
    zval zval_args;
    if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SSz", &name, &age, &zval_args) == FAILURE){
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "参数接受错误");
        RETURN_FALSE;
    }

    php_printf("zval_args的type类型:%d\n", Z_TYPE(zval_args));
    ZVAL_STR(&zval_args, name);
    php_printf("将name复制后的zval_args的type类型:%d\n",Z_TYPE (zval_args));
    php_printf("named:%s\n",name->val);
    php_printf("age:%s\n",age->val);
    zval_ptr_dtor(&zval_args);
}</code>
Copy after login
Copy after login

PHP code

<code><?php
$br = (php_sapi_name() == "cli")? "":"<br>";

if(!extension_loaded('helloZval')) {
    dl('helloZval.' . PHP_SHLIB_SUFFIX);
}
debug_zval("sss",2,4);

?>
</code>
Copy after login
Copy after login

Output

<code>zval_args的type类型:0    (IS_UNDF)
将name复制后的zval_args的type类型:6(IS_STRING)                                                                                  
named:sss
age:2</code>
Copy after login
Copy after login

I have two doubts

  • When I use zval as a container for receiving parameters (let’s call it that), why do I not receive the parameters successfully, but IS_UNDEF

  • If zval cannot be used as a parameter container, then zend_string has other types that are defined at the same time (such as zend_array, zend_object....) What is the difference between zend_string and zval (both zend_string and zval can be GCed and zend_string is more lightweight) , or what is the difference between the usage of zval and zend_string?

Reply content:

My extension code is like this

<code>ZEND_BEGIN_ARG_INFO_EX(helloZvalArg, 0, 0, 1)
    ZEND_ARG_INFO (0, name)
    ZEND_ARG_INFO (0, age)
    ZEND_ARG_INFO (0, zval_args)
ZEND_END_ARG_INFO()



PHP_FUNCTION(debug_zval)
{
    zend_string *name, *age;
    zval zval_args;
    if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SSz", &name, &age, &zval_args) == FAILURE){
        php_error_docref(NULL TSRMLS_CC, E_WARNING, "参数接受错误");
        RETURN_FALSE;
    }

    php_printf("zval_args的type类型:%d\n", Z_TYPE(zval_args));
    ZVAL_STR(&zval_args, name);
    php_printf("将name复制后的zval_args的type类型:%d\n",Z_TYPE (zval_args));
    php_printf("named:%s\n",name->val);
    php_printf("age:%s\n",age->val);
    zval_ptr_dtor(&zval_args);
}</code>
Copy after login
Copy after login

PHP code

<code><?php
$br = (php_sapi_name() == "cli")? "":"<br>";

if(!extension_loaded('helloZval')) {
    dl('helloZval.' . PHP_SHLIB_SUFFIX);
}
debug_zval("sss",2,4);

?>
</code>
Copy after login
Copy after login

Output

<code>zval_args的type类型:0    (IS_UNDF)
将name复制后的zval_args的type类型:6(IS_STRING)                                                                                  
named:sss
age:2</code>
Copy after login
Copy after login

I have two doubts

  • When I use zval as a container for receiving parameters (let’s call it that), why do I not receive the parameters successfully, but IS_UNDEF

  • If zval cannot be used as a parameter container, then zend_string has other types that are defined at the same time (such as zend_array, zend_object....) What is the difference between zend_string and zval (both zend_string and zval can be GCed and zend_string is more lightweight) , or what is the difference between the usage of zval and zend_string?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template