C Extension calls PHP function (call C function to implement PHP function)

Source: Internet
Author: User

The last is written C extension call C's standard function, but only to invoke the function declared in the header file, today, the C extension calls the implementation of the PHP function C function, for example, C extension to use PHP Ip2long this function, but C can not be called in PHP, It's definitely going to call the C function that implements the PHP function. So how does the C extension invoke the C kernel-to-PHP API?

A function is used here: Zend_api int call_user_function_ex (HashTable *function_table, Zval **object_pp, Zval *function_name, Zval * * Retval_ptr_ptr, Zend_uint Param_count, Zval **params[], int no_separation, HashTable *symbol_table tsrmls_dc);

The first argument is that Hashtable,zend uses Hashtable to store PHP functions, and function_table is used to specify which hashtable to get the function from. Usually should use CG (function_table), expand is Compiler_globals.function_table,compiler_globals is a to store the compiler data of the global structure (and its corresponding to an eg macro, That is executor_globals, which is used to store actuator data). Compiler_globals.function_table stores all of the functions that we can invoke within the PHP page, including Zend, PHP standard library functions, module-derived functions, and functions defined by the user using PHP code.

OBJECT_PP is an object that, when specified, Zend gets the function from the object's function table, which is not discussed and is always set to null.

Function_name must be a string-type Zval that stores the name of the function we want to invoke. Why use Zval instead of using char* directly, because Zend consider that most of the time, we all get parameters from the user, and then call CALL_USER_FUNCTION_EX, so that we can not be processed directly to the user parameters to the function. Of course, we can also manually create a string type Zval to pass it.

Retval_ptr_ptr is used to get the return value of a function, and after Zend executes the specified function, it fills the pointer of the return value here. The space function of this container will automatically help you apply, so we do not need to apply manually, but in the event of this container space destruction release work by ourselves (using zval_dtor ()) to do.

Param_count and params are used to specify the parameters of the function, Param_count is an integer that identifies the number of arguments, params[] is an array that contains the specific parameters.

The no_separation is used to specify whether Zval detach is performed when necessary, which occurs when writing to a non-reference zval. It should always be set to 0, which means performing a zval separation, or it might corrupt the data.

Symbol_table is used to specify the active_symbol_table of the target function, which should normally be null, so that Zend generates an empty symbol table for the target function. Let's look at a specific example:

I want to use the Ip2long function in the PHP function in the C extension, then call the method

unsigned long ip2longs (const char* IP) {

Zval *funname,*ret_ptr = null,*args,**params[1];
Make_std_zval (Funname); Creating variables
Zval_string (Funname, "Ip2long", 1); Set the type and value of the Zval, and the second parameter is the Ip2long function that we want to call, which two methods do not understand can be viewed http://www.cunmou.com/phpbook/2.3.md

Make_std_zval (args);
Zval_string (args,ip,1);

Params[0] = &args; Put the parameters into the array

CALL_USER_FUNCTION_EX (eg (function_table), NULL, Funname, &ret_ptr, 1, params, 0, eg (active_symbol_table)); Calling functions

Zval_ptr_dtor (&RET_PTR); Destroying a manually created space

Return ret_ptr->value.lval; Gets the value returned because the function returned is a long type, so take lval
}

This function is a function that PHP can call directly. function is used to determine if an IP is in the intranet

Php_function (is_intranet) {
Char *ip;
int ip_length=0;
if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "s", &ip,&ip_length) = = FAILURE) {
Return_null ();
}
unsigned long Ip_long = ip2longs (IP); //Call the declared function
if (Ip_long = = Ip2longs ("127.0.0.1") | | (Ip_long > Ip2longs ("10.0.0.0") && ip_long <= ip2longs ("10.255.255.255")) | |
(Ip_long >= ip2longs ("172.16.0.0") && ip_long <= ip2longs ("172.31.255.255")) | |
(Ip_long >= ip2longs ("192.168.0.0") && ip_long <= ip2longs ("192.168.255.255"))
) {
Return_bool (1);
} else {
Return_bool (0);
}
}

Here in more zval structure, these are their usual study summary, here to share with you:

The basic unit of storage in PHP is Zval (Zend Value), zval structure, in fact all user-defined variables in PHP are represented by the Zval type, when we use the Zend_parse_parameters function to parse parameters, The Zend engine casts the type based on the corresponding data type, and because of the array, object, and resource types in PHP, there is no corresponding type in C, so type conversion is not possible, and they are all represented by Zval. struct _zval_struct {zvalue_value value;
unsigned char type;
unsigned char is_ref;
Short RefCount;
};typedef struct _zval_struct zval; struct field Interpretation: (1)Zval_value Value

The actual value of the variable, specifically a Zvalue_value Union (union):

typedef Union _ZVALUE_VALUE {
Long lval; /* Long value */
Double Dval; /* Double Value */
struct {/* String */
Char *val;
int Len;
} str;
HashTable *ht; /* Hash table value,used for array */
Zend_object_value obj; /* Object */} Zvalue_value;

The description of the ZVALUE_VALUE structure is as follows:

Lval If the variable type is Is_long, Is_boolean, or is_resource, use this property value
Dval If the variable type is is_double, use this property value
STR accesses this property value if the variable type is is_string. Its field Len indicates the length of the string, and the field Val points to the string. Since Zend uses a C-style string, the length of the string must also be counted as Terminator 0x00 at the end of the string.
HT If the variable type is an array, then this HT points to the hash table of the arrays entry obj if the variable type is is_object, use this attribute value (2) type Zval *uservar; if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "Z",
&uservar) = = FAILURE) {
Return_null (); The value of the type of the struct is as follows: Gets the type through z_type_p (Uservar); Is_null represents a null value
Is_long is a (long) integer
Is_double is a double-precision floating-point number
Is_string is a string
Is_array is an array
Is_object is an object
Is_bool is a Boolean value
Is_resource is a resource (for discussion of resources, we will discuss it later in due course) is_string is a constant (3) Is_ref 0 indicates that the variable is not yet a reference. 1 means that the variable is also referenced by another variable (4) refcount Indicates whether the variable is still valid. For each additional reference to this variable, this value is incremented by 1. Conversely, each time a reference to this variable is lost, the value is reduced by 1. When the reference count is reduced to 0, it means there is no reference to the variable, and the variable is automatically released

C Extension calls PHP function (call C function to implement PHP function)

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.