text
stringlengths
1
254
output
stringlengths
2
255
CONST_PERSISTENT - This constant is persistent and won' t be "forgotten "when the current process carrying this constant shuts down.
CONST_CS - This constant's name is to be treated as case sensitive.
Table 33-6.
CONST_PERSISTENT - This constant is persistent and won't be "forgotten" when the current process carrying this constant shuts down.
The specified string must reside in Zend 's internal memory.
表 33-6Macros for Creating Constants
The member in the zval.value container is dval; the corresponding type is IS_DOUBLE.
Doubles are PHP's floats and are as easy to assign as longs, because their value is also contained directly in the union. The member in the zval.value container is dval; the corresponding type is IS_DOUBLE.
They don' t need any zval allocation; you simply have to supply a variable name and value.
表 33-5Macros for Global Variable Creation
This section shows you how to deal with the variable types that PHP supports.
When exchanging data from your own extensions with PHP scripts, one of the most important issues is the creation of variables. This section shows you how to deal with the variable types that PHP supports.
By specifying EG( active_symbol_table), you get access to the currently active symbol table, dealing with the active, local scope.
If you need to optimize for speed and don't care about optimal memory usage, you can omit the check for an existing variable with the same value and instead force insertion into the symbol table by using zend_hash_update():
If you need to optimize for speed and don' t care about optimal memory usage, you can omit the check for an existing variable with the same value and instead force insertion into the symbol table by using zend_hash_update():
The variables generated with the snippet above will always be of local scope, so they reside in the context in which the function has been called. To create new variables in the global scope, use the same method but refer to another symbol table:
To create new variables in the global scope, use the same method but refer to another symbol table:
Note: The active_symbol_table variable is a pointer, but symbol_table is not. This is why you have to use EG(active_symbol_table) and EG(symbol_table) as parameters to ZEND_SET_SYMBOL - it requires a pointer.
The active_symbol_table variable is a pointer, but symbol_table is not.
Similarly, to get a more efficient version, you can hardcode the symbol table update:
Similarly, to get a more efficient version, you can hardcode the symbol table update:
Note: You can see that the global variable is actually not accessible from within the function. This is because it's not imported into the local scope using global $global_variable; in the PHP source.
This is because it' s not imported into the local scope using global $global_variable; in the PHP source.
例 33-1Creating variables with different scopes.
zval *new_long; MAKE_STD_ZVAL(new_long); ZVAL_LONG(new_long, 10);
例 33-2Creation of a long.
Objects are maintained with the same hash functions, but there 's a different API for creating them.
Since objects can be converted to arrays (and vice versa), you might have already guessed that they have a lot of similarities to arrays in PHP. Objects are maintained with the same hash functions, but there's a different API for creating them.
zval *new_object; MAKE_STD_ZVAL(new_object); if(object_init(new_object) != SUCCESS) {/ / do error handling here}
To initialize an object, you use the function object_init():
This is useful if you have to add properties which aren 't simple types like integers or strings but arrays or other objects.
表 33-4Zend's API for Object Creation
To create a new resource you need to register a resource destruction handler for it.
For example, resources are used to store database links and file descriptors. The de facto standard implementation can be found in the MySQL module, but other modules such as the Oracle module also make use of resources.
This works by registering your own resource destruction handler to Zend which in turn gets called by Zend whenever your resource can be freed (whether manually or automatically).
注意 In fact, a resource can be a pointer to anything you need to handle in your functions (e.g. pointer to a structure) and the user only has to pass a single resource variable to your function.
The Zend function to register your resource handler is defined as:
The Zend function to register your resource handler is defined as:
When registering a resource, either of these handlers must be given.
zend_register_list_destructors_ex() accepts the following parameters:
zend_register_list_destructors_ex() accepts the following parameters:
The resource destruction handler (either normal or persistent resources) has the following prototype:
It' s always a good thing to specify an unique name within PHP for the resource type so when the user for example calls var_dump($resource); he also gets the name of the resource.
Now we know how to start things, we define our own resource we want register within Zend. It is only a simple structure with two integer members:
The resource destruction handler (either normal or persistent resources) has the following prototype:
注意 One important thing to mention: If your resource is a rather complex structure which also contains pointers to memory you allocated during runtime you have to free them before freeing the resource itself!
rsrc_id = zend_list_insert( rsrc_pointer, rsrc_type); if (rsrc_result) {rsrc_result - value.lval = rsrc_id; rsrc_result - type = IS_RESOURCE;} return rsrc_id;
What is really going on when you register a new resource is it gets inserted in an internal list in Zend and the result is just stored in the given zval * container:
Zend now keeps track of all references to this resource.
注意 It is common practice that if you want to return the resource immidiately to the user you specify the return_value as the zval * container.
new_string - type = IS_STRING; new_string - value.str.len = 0; new_string - value.str.val = empty_string;
To create empty strings, set the string length to 0 and use empty_string as contents: