In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
PHP extension development of what function return value, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
The macro definition provided in API is incorrect to some extent. In fact, the return values of all functions in the PHP extension are passed through a variable called return_value.
Recursively, this variable is also a parameter in the function, defined in the PHP_FUNCTION () prototype. This parameter always contains a zval that applies for space in advance (for zval is
The more complex part of PHP extension development will be described later in the article on receive parameters) the container and assign it to NULL, so even if we don't declare return_value, we can
Assign a value to it by directly accessing its members.
In a PHP extension function, setting the return value of the function is correct in the following five ways:
RETURN_LONG (42); RETVAL_LONG (42); return;ZVAL_LONG (return_value, 42); return;Z_TYPE_P (return_value) = IS_LONG;Z_LVAL_P (return_value) = 42
Macros such as ZVAL_LONG, Z_TYPE_P and so on will be described in later articles. In fact, we can see that RETURN_LONG is a re-encapsulation of RETVAL_LONG, without our explicit return. As introduced in the first Quick start, Zend API provides a large number of macro definitions for function return values:
RETURN_xxx series macro definition:
RETURN_RESOURCE (resource) returns a resource RETURN_BOOL (bool) returns a Boolean value RETURN_NULL () returns a null value RETURN_LONG (long) returns a long integer RETURN_DOUBLE (double) returns a double-precision floating point number RETURN_STRING (string, duplicate) returns a string. Duplicate indicates whether the character is copied using strdup () RETURN_STRINGL (string, length, duplicate) returns a string of fixed length. The rest is the same as RETURN_STRING. This macro is faster and binary-safe RETURN_EMPTY_STRING () returns an empty string RETURN_FALSE returns a Boolean false RETURN_TRUE returns a Boolean true
RETVAL_xxx series macro definition:
RETVAL_RESOURCE (resource) set the return value to a specified resource RETVAL_BOOL (bool) set the return value to a specified Boolean value RETVAL_NULL set the return value to null RETVAL_LONG (long) set the return value to a specified long integer RETVAL_DOUBLE (double) set the return value to a specified double-precision floating-point number RETVAL_STRING (string, duplicate) set the return value to a specified string The meaning of duplicate is the same as that of RETURN_STRINGRETVAL_STRINGL (string, length, duplicate). The return value is a specified string of fixed length. The rest is the same as RETVAL_STRING. This macro is faster and binary safe RETVAL_EMPTY_STRING setting returns an empty string RETVAL_FALSE setting returns a Boolean false RETVAL_TRUE setting returns a Boolean True
In addition to using the RETURN_xxx series macro definition, we need to explicitly use the declare statement in all other ways. If you need to return complex classes such as arrays and objects
Type data, you need to call array_init () and object_init () first, or you can use the corresponding hash function to manipulate return_value directly, because
This process is complicated, and there are no corresponding macros to assist us, which will be covered later in the introduction of arrays and object types. Let's take a look at an example, using the five ways we mentioned above.
Add a function declaration to the header file:
PHP_FUNCTION (return_value_way1); PHP_FUNCTION (return_value_way2); PHP_FUNCTION (return_value_way3); PHP_FUNCTION (return_value_way4); PHP_FUNCTION (return_value_way5)
Implement the function:
PHP_FUNCTION (return_value_way1) {RETURN_LONG (42);} PHP_FUNCTION (return_value_way2) {RETVAL_LONG (42); return;} PHP_FUNCTION (return_value_way3) {ZVAL_LONG (return_value, 42); return;} PHP_FUNCTION (return_value_way4) {Z_TYPE_P (return_value) = IS_LONG; Z_LVAL_P (return_value) = 42; return } PHP_FUNCTION (return_value_way5) {return_value- > type = IS_LONG; return_value- > value.lval = 42; return;}
Register the extension functions to the function table. None of these functions requires parameters, which are expressed as NULL:
Const zend_function_entry fetion_echo_functions [] = {PHP_FE (say_goodbye, arg_say_goodbye) PHP_FE (say_hello, NULL) PHP_FE (return_value_way1, NULL) PHP_FE (return_value_way2, NULL) PHP_FE (return_value_way3, NULL) PHP_FE (return_value_way4, NULL) PHP_FE (return_value_way5, NULL) {NULL, NULL, NULL}}
Finally, compile the extension, write a simple test script and run it.
That's all about the return value of the PHP extension function.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.