[ Pobierz całość w formacie PDF ]
.Przykład 30-1 shows thesource.Przykład 30-1.A simple extension./* include standard header */#include "php.h"/* declaration of functions to be exported */ZEND_FUNCTION(first_module);/* compiled function list so Zend knows what s in this module */zend_function_entry firstmod_functions[] ={ZEND_FE(first_module, NULL){NULL, NULL, NULL}};/* compiled module information */zend_module_entry firstmod_module_entry ={STANDARD_MODULE_HEADER,"First Module",firstmod_functions,NULL,NULL,NULL,NULL,NULL,NO_VERSION_YET,STANDARD_MODULE_PROPERTIES};/* implement standard "stub" routine to introduce ourselves to Zend */#if COMPILE_DL_FIRST_MODULEZEND_GET_MODULE(firstmod)#endif/* implement function that is meant to be made available to PHP */ZEND_FUNCTION(first_module){long parameter;if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", ¶meter) == FAIL-URE) {return;}RETURN_LONG(parameter);}This code contains a complete PHP module.We ll explain the source code in detail shortly, but firstwe d like to discuss the build process.(This will allow the impatient to experiment before we diveinto API discussions.)1760 Rozdział 30.Creating ExtensionsNotatka: The example source makes use of some features introduced with the Zend versionused in PHP 4.1.0 and above, it won t compile with older PHP 4.x versions.Compiling ModulesThere are basically two ways to compile modules:" Use the provided "make" mechanism in theextdirectory, which also allows building of dynamicloadable modules." Compile the sources manually.The first method should definitely be favored, since, as of PHP 4.0, this has been standardized into asophisticated build process.The fact that it is so sophisticated is also its drawback, unfortunately -it s hard to understand at first.We ll provide a more detailed introduction to this later in the chapter,but first let s work with the default files.The second method is good for those who (for some reason) don t have the full PHP source treeavailable, don t have access to all files, or just like to juggle with their keyboard.These cases shouldbe extremely rare, but for the sake of completeness we ll also describe this method.Compiling Using Make.To compile the sample sources using the standard mechanism, copy alltheir subdirectories to theextdirectory of your PHP source tree.Then runbuildconf, which willcreate an updatedconfigurescript containing appropriate options for the new extension.Bydefault, all the sample sources are disabled, so you don t have to fear breaking your build process.After you runbuildconf,configure --helpshows the following additional modules:--enable-array_experiments BOOK: Enables array experiments--enable-call_userland BOOK: Enables userland module--enable-cross_conversion BOOK: Enables cross-conversion module--enable-first_module BOOK: Enables first module--enable-infoprint BOOK: Enables infoprint module--enable-reference_test BOOK: Enables reference test module--enable-resource_test BOOK: Enables resource test module--enable-variable_creation BOOK: Enables variable-creation moduleThe module shown earlier in Przykład 30-1 can be enabled with--enable-first_moduleor--enable-first_module=yes.Compiling Manually.To compile your modules manually, you need the following commands:Action CommandCompiling cc -fpic -DCOMPILE_DL=1 -I/usr/local/include -I.-I.-I./Zend -c -otype = IS_LONG;new_long->value.lval = 10;1790 Rozdział 35.Creating VariablesAlternatively, you can use the macroZVAL_LONG:zval *new_long;MAKE_STD_ZVAL(new_long);ZVAL_LONG(new_long, 10);Doubles (Floats)Doubles are PHP s floats and are as easy to assign as longs, because their value is also containeddirectly in the union.The member in the zval.value container is dval; the corresponding type isIS_DOUBLE.zval *new_double;MAKE_STD_ZVAL(new_double);new_double->type = IS_DOUBLE;new_double->value.dval = 3.45;Alternatively, you can use the macroZVAL_DOUBLE:zval *new_double;MAKE_STD_ZVAL(new_double);ZVAL_DOUBLE(new_double, 3.45);StringsStrings need slightly more effort.As mentioned earlier, all strings that will be associated with Zend sinternal data structures need to be allocated using Zend s own memory-management functions.Referencing of static strings or strings allocated with standard routines is not allowed.To assignstrings, you have to access the structure str in the zval.value container.The corresponding type isIS_STRING:zval *new_string;char *string_contents = "This is a new string variable";MAKE_STD_ZVAL(new_string);new_string->type = IS_STRING;new_string->value.str.len = strlen(string_contents);new_string->value.str.val = estrdup(string_contents);1791 Rozdział 35.Creating VariablesNote the usage of Zend s estrdup() here.Of course, you can also use the predefined macroZVAL_STRING:zval *new_string;char *string_contents = "This is a new string variable";MAKE_STD_ZVAL(new_string);ZVAL_STRING(new_string, string_contents, 1);ZVAL_STRINGaccepts a third parameter that indicates whether the supplied string contents shouldbe duplicated (using estrdup()).Setting this parameter to1causes the string to be duplicated;0simply uses the supplied pointer for the variable contents.This is most useful if you want to create anew variable referring to a string that s already allocated in Zend internal memory.If you want to truncate the string at a certain position or you already know its length, you can useZVAL_STRINGL(zval, string, length, duplicate), which accepts an explicit string lengthto be set for the new string.This macro is faster thanZVAL_STRINGand also binary-safe.To create empty strings, set the string length to0and useempty_stringas contents:new_string->type = IS_STRING;new_string->value.str.len = 0;new_string->value.str.val = empty_string;Of course, there s a macro for this as well (ZVAL_EMPTY_STRING):MAKE_STD_ZVAL(new_string);ZVAL_EMPTY_STRING(new_string);BooleansBooleans are created just like longs, but have the typeIS_BOOL.Allowed values in lval are0and1:zval *new_bool;MAKE_STD_ZVAL(new_bool);new_bool->type = IS_BOOL;new_bool->value.lval = 1;The corresponding macros for this type areZVAL_BOOL(allowing specification of the value) as wellasZVAL_TRUEandZVAL_FALSE(which explicitly set the value toTRUEandFALSE, respectively).ArraysArrays are stored using Zend s internal hash tables, which can be accessed using the zend_hash_*()API.For every array that you want to create, you need a new hash table handle, which will be storedin the ht member of the zval.value container.1792 Rozdział 35.Creating VariablesThere s a whole API solely for the creation of arrays, which is extremely handy.To start a new array,you call array_init().zval *new_array;MAKE_STD_ZVAL(new_array);if(array_init(new_array) != SUCCESS){// do error handling here}If array_init() fails to create a new array, it returnsFAILURE.To add new elements to the array, you can use numerous functions, depending on what you want todo.Tabela 35-1, Tabela 35-2 and Tabela 35-3 describe these functions.All functions returnFAILUREon failure andSUCCESSon success.Tabela 35-1.Zend s API for Associative ArraysFunction Descriptionadd_assoc_long(zval *array, char *key, long Adds an element of typelong.n);()add_assoc_unset(zval *array, char *key);() Adds an unset element.add_assoc_bool(zval *array, char *key, int Adds a Boolean element.b);()add_assoc_resource(zval *array, char *key, intAdds a resource to the array.r);()add_assoc_double(zval *array, char *key, Adds a floating-point value.double d);()add_assoc_string(zval *array, char *key, char Adds a string to the array.The flag duplicate*str, int duplicate);() specifies whether the string contents have to becopied to Zend internal memory [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • agnieszka90.opx.pl