VS2019 开发PHP 拓展(四)创建 function
无参无返回
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test1, 0, 0, IS_VOID, 0)
ZEND_END_ARG_INFO()
/* {{{ void test1()
*/
PHP_FUNCTION(test1)
{
ZEND_PARSE_PARAMETERS_NONE();
php_printf("The extension %s is loaded and working!\r\n", "zodream");
}
/* }}} */
static const zend_function_entry zodream_functions[] = {
PHP_FE(test1, arginfo_test1)
PHP_FE_END
};
1参无返回
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test2, 0, 0, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_END_ARG_INFO()
/* {{{ void test2()
*/
PHP_FUNCTION(test2)
{
char *var = "World";
size_t var_len = sizeof("World") - 1;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(var, var_len)
ZEND_PARSE_PARAMETERS_END();
php_printf("Hello %s\r\n", var);
}
/* }}} */
static const zend_function_entry zodream_functions[] = {
PHP_FE(test2, arginfo_test2)
PHP_FE_END
};
多参无返回
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test3, 0, 0, IS_VOID, 2)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, config, IS_ARRAY, 0)
ZEND_END_ARG_INFO()
/* {{{ void test3(string $str, array $config)
*/
PHP_FUNCTION(test3)
{
zval* config, *entry;
zend_ulong num_idx;
zend_string* str_idx;
char* var = "World";
size_t var_len = sizeof("World") - 1;
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_STRING(var, var_len)
Z_PARAM_ARRAY(config)
ZEND_PARSE_PARAMETERS_END();
php_printf("Hello %s\r\n", var);
// 循环打印为字符串的值
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(config), num_idx, str_idx, entry) {
ZVAL_DEREF(entry);
if (Z_TYPE_P(entry) == IS_STRING) {
php_printf("Hello %s\r\n", Z_STRVAL_P(entry));
}
} ZEND_HASH_FOREACH_END();
}
/* }}} */
static const zend_function_entry zodream_functions[] = {
PHP_FE(test3, arginfo_test3)
PHP_FE_END
};
无参有返回
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test4, 0, 0, IS_STRING, 0)
ZEND_END_ARG_INFO()
/* {{{ void test4()
*/
PHP_FUNCTION(test4)
{
ZEND_PARSE_PARAMETERS_NONE();
zend_string* retval = strpprintf(0, "test 4");
RETURN_STR(retval);
}
/* }}} */
static const zend_function_entry zodream_functions[] = {
PHP_FE(test4, arginfo_test4)
PHP_FE_END
};
有参有返回
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_test5, 0, 0, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, str, IS_STRING, 0)
ZEND_END_ARG_INFO()
/* {{{ void test5()
*/
PHP_FUNCTION(test5)
{
char* var = "World";
size_t var_len = sizeof("World") - 1;
zend_string* retval;
ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_STRING(var, var_len)
ZEND_PARSE_PARAMETERS_END();
retval = strpprintf(0, "Hello %s", var);
RETURN_STR(retval);
}
/* }}} */
static const zend_function_entry zodream_functions[] = {
PHP_FE(test5, arginfo_test5)
PHP_FE_END
};
转载请保留原文链接: https://zodream.cn/blog/id/105.html