December 4th, 2007
Accepting Arguments 325 Entry Description is_ref 0 means that this variable is not a reference; 1 means that this variable is a reference to another variable. refcount The number of references that exist for this variable. For every new reference to the value stored in this variable, this counter is increased by 1. For every lost reference, this counter is decreased by 1.When the reference counter reaches 0, no references exist to this value anymore, which causes automatic freeing of the value. Table 9.6 Zend zvalue_value Structure Entry Description lval Use this property if the variable is of the type IS_LONG, IS_BOOLEAN, or IS_RESOURCE. dval Use this property if the variable is of the type IS_DOUBLE. str This structure can be used to access variables of the type IS_STRING.The member len contains the string length; the member val points to the string itself. Zend uses C strings; thus, the string length contains a trailing 0×00. ht This entry points to the variable s hash table entry if the variable is an array. obj Use this property if the variable is of the type IS_OBJECT. Table 9.7 Zend Variable Type Constants Constant Description IS_NULL Denotes a NULL (empty) value. IS_LONG A long (integer) value. IS_DOUBLE A double (floating point) value. IS_STRING A string. IS_ARRAY Denotes an array. IS_OBJECT An object. IS_BOOL A Boolean value. IS_RESOURCE A resource (for a discussion of resources, see the appropriate section below). IS_CONSTANT A constant (defined) value.
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.
Posted in MySQL | No Comments »
December 4th, 2007
324 Chapter 9 Extending PHP 4.0: Hacking the Core of PHP After retrieving the parameter pointer, the parameter value is converted to a long (an integer), which also forms the return value of this function. Understanding access to the contents of the value requires a short discussion of the zval type, whose definition is shown in Listing 9.8. Listing 9.8 PHP/Zend zval type definition. typedef pval zval; typedef struct _zval_struct zval; typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ struct { zend_class_entry *ce; HashTable *properties; } obj; } zvalue_value; struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ unsigned char type; /* active type */ unsigned char is_ref; short refcount; }; Actually, pval (defined in php.h) is only an alias of zval (defined in zend.h), which in turn refers to _zval_struct.This is a most interesting structure. _zval_struct is the master structure,containing the value structure,type,and reference information.The substructure zvalue_value is a union that contains the variable s contents. Depending on the variable s type, you ll have to access different members of this union. For a description of both structures, see Tables 9.5, 9.6, and 9.7. Table 9.5 Zend zval Structure Entry Description value Union containing this variable s contents. See Table 9.6 for a description. type Contains this variable s type. For a list of available types, see Table 9.7.
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.
Posted in MySQL | No Comments »
December 3rd, 2007
Accepting Arguments 323 Figure 9.5 Cross-conversion behavior of PHP. Using these functions on your arguments will ensure type safety for all data that s passed to you. If the supplied type doesn t match the required type, PHP forces dummy contents on the resulting value (empty strings, arrays, or objects, 0 for numeric values, FALSE for Booleans) to ensure a defined state. Following is a quote from the sample module discussed previously, which makes use of the conversion functions: zval **parameter; if((ZEND_NUM_ARGS() != 1) || (zend_get_parameters_ex(1, ¶meter) != SUCCESS)) { WRONG_PARAM_COUNT; } convert_to_long_ex(parameter); RETURN_LONG((*parameter)->value.lval);
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.
Posted in MySQL | No Comments »
December 3rd, 2007
322 Chapter 9 Extending PHP 4.0: Hacking the Core of PHP Table 9.4 Continued Function Description convert_to_double_ex(value) Forces conversion to a double, the default floating-point type. NULL values, Booleans, resources, longs, and of course doubles remain untouched. Strings containing a number are converted to their corresponding numeric representation, otherwise resulting in 0.0.Arrays and objects are converted to 0.0 if empty, 1.0 otherwise. convert_to_string_ex(value) Forces conversion to a string. Strings remain untouched. NULL values are converted to an empty string. Booleans containing TRUE are converted to 1 , otherwise resulting in an empty string. Longs and doubles are converted to their corresponding string representation. Arrays are converted to the string Array and objects to the string Object . convert_to_array_ex(value) Forces conversion to an array. Arrays remain untouched. Objects are converted to an array by assigning all their properties to the array table.All property names are used as keys, property contents as values. NULL values are converted to an empty array.All other values are converted to an array that contains the specific source value in the element with the key 0. convert_to_object_ex(value) Forces conversion to an object. Objects remain untouched. NULL values are converted to an empty object. Arrays are converted to objects by introducing their keys as properties into the objects and their values as corresponding property contents in the object. All other types result in an object with the property scalar, having the corresponding source value as content. convert_to_null_ex(value) Forces the type to become a NULL value, meaning empty. Note: You can find a demonstration of the behavior in cross_conversion.php on the accompanying CD-ROM. Figure 9.5 shows the output.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.
Posted in MySQL | No Comments »
December 2nd, 2007
Accepting Arguments 321 fsockopen() accepts two,three,four,or five parameters.After the obligatory variable declarations, the function checks for the correct range of arguments.Then it uses a fall-through mechanism in a switch() statement to deal with all arguments.The switch() statement starts with the maximum number of arguments being passed (five). After that, it automatically processes the case of four arguments being passed, then three, by omitting the otherwise obligatory break keyword in all stages.After having processed the last case, it exits the switch() statement and does the minimal argument processing needed if the function is invoked with only two arguments. This multiple-stage type of processing, similar to a stairway, allows convenient processing of a variable number of arguments. Accessing Arguments To access arguments, it s necessary for each argument to have a clearly defined type. Again, PHP s extremely dynamic nature introduces some quirks. Because PHP never does any kind of type checking, it s possible for a caller to pass any kind of data to your functions, whether you want it or not. If you expect an integer, for example, the caller might pass an array, and vice versa PHP simply won t notice. To work around this, you have to use a set of API functions to force a type conversion on every argument that s being passed (see Table 9.4). Note: All conversion functions expect a **zval as parameter. Table 9.4 Argument Conversion Functions Function Description convert_to_boolean_ex(value) Forces conversion to a Boolean type. Boolean values remain untouched. Longs, doubles, and strings containing 0 as well as NULL values will result in Boolean 0 (FALSE).Arrays and objects are converted based on the number of entries or properties, respectively, that they have. Empty arrays and objects are converted to FALSE;otherwise,to TRUE.All other values result in a Boolean 1 (TRUE). convert_to_long_ex(value) Forces conversion to a long, the default integer type. NULL values, Booleans, resources, and of course longs remain untouched. Doubles are truncated. Strings containing an integer are converted to their corresponding numeric representation, otherwise resulting in 0.Arrays and objects are converted to 0 if empty, 1 otherwise. continues
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.
Posted in MySQL | No Comments »
December 2nd, 2007
320 Chapter 9 Extending PHP 4.0: Hacking the Core of PHP Listing 9.7 PHP s implementation of variable arguments in fsockopen( ). pval **args[5]; int *sock=emalloc(sizeof(int)); int *sockp; int arg_count=ARG_COUNT(ht); int socketd = -1; unsigned char udp = 0; struct timeval timeout = { 60, 0 }; unsigned short portno; unsigned long conv; char *key = NULL; FLS_FETCH(); if (arg_count > 5 || arg_count < 2 || .zend_get_parameters_array_ex(arg_count,args)==FAILURE) { CLOSE_SOCK(1); WRONG_PARAM_COUNT; } switch(arg_count) { case 5: convert_to_double_ex(args[4]); conv = (unsigned long) ((*args[4])->value.dval * 1000000.0); timeout.tv_sec = conv / 1000000; timeout.tv_usec = conv % 1000000; /* fall-through */ case 4: if(!ParameterPassedByReference(ht,4)) { php_error(E_WARNING, error string argument to fsockopen not passed by .reference ); } pval_copy_constructor(*args[3]); (*args[3])->value.str.val = empty_string; (*args[3])->value.str.len = 0; (*args[3])->type = IS_STRING; /* fall-through */ case 3: if(!ParameterPassedByReference(ht,3)) { php_error(E_WARNING, error argument to fsockopen not passed by .reference ); } (*args[2])->type = IS_LONG; (*args[2])->value.lval = 0; break; } convert_to_string_ex(args[0]); convert_to_long_ex(args[1]); portno = (unsigned short) (*args[1])->value.lval; key = emalloc((*args[0])->value.str.len + 10);
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.
Posted in MySQL | No Comments »
December 1st, 2007
Accepting Arguments 319 The return value of zend_get_parameters_ex() can either be SUCCESS or FAILURE, indicating (unsurprisingly) success or failure of the argument processing.A failure is most likely related to an incorrect number of arguments being specified, in which case you should exit with WRONG_PARAMETER_COUNT. To retrieve more than one argument, you can use a similar snippet: zval **param1, **param2, **param3, **param4; if(zend_get_parameters_ex(4, ¶m1, ¶m2, ¶m3, ¶m4) != SUCCESS) WRONG_PARAMETER_COUNT; zend_get_parameters_ex() only checks whether you re trying to retrieve too many parameters. If the function is called with five arguments, but you re only retrieving three of them with zend_get_parameters_ex(), you won t get an error but will get the first three parameters instead. Subsequent calls of zend_get_parameters_ex() won t retrieve the remaining arguments, but will get the same arguments again. Dealing with a Variable Number of Arguments/Optional Parameters If your function is meant to accept a variable number of arguments, the snippets just described are sometimes suboptimal solutions.You have to create a line calling zend_get_parameters_ex() for every possible number of arguments, which is often unsatisfying. For this case, you can use the function zend_get_parameters_array_ex(), which accepts the number of parameters to retrieve and an array in which to store them: zval **parameter_array[4]; /* get the number of arguments */ argument_count = ZEND_NUM_ARGS(); /* see if it satisfies our minimal request (2 arguments) */ /* and our maximal acceptance (4 arguments) */ if(argument_count < 2 || argument_count > 5) WRONG_PARAMETER_COUNT; /* argument count is correct, now retrieve arguments */ if(zend_get_parameters_array_ex(argument_count, parameter_array) != SUCCESS) WRONG_PARAMETER_COUNT; First, the number of arguments is checked to make sure that it s in the accepted range. After that, zend_get_parameters_array_ex() is used to fill parameter_array with valid pointers to the argument values. A very clever implementation of this can be found in the code handling PHP s fsockopen() located in ext/standard/fsock.c, as shown in Listing 9.7. Don t worry if you don t know all the functions used in this source yet; we ll get to them shortly.
We recommend high quality webhost to host and run your jsp application: christian web host services.
Posted in MySQL | No Comments »
December 1st, 2007
318 Chapter 9 Extending PHP 4.0: Hacking the Core of PHP Figure 9.4 WRONG_PARAMETER_COUNT in action. This macro prints a default error message and then returns to the caller. Its definition can also be found in zend_API.h and looks like this: ZEND_API void wrong_param_count(void); #define WRONG_PARAM_COUNT { wrong_param_count(); return; } As you can see, it calls an internal function named wrong_param_count() that s responsible for printing the warning. For details on generating customized error messages, see the later section Printing Information. Retrieving Arguments After having checked the number of arguments, you need to get access to the arguments themselves.This is done with the help of zend_get_parameters_ex(): zval **parameter; if(zend_get_parameters_ex(1, ¶meter) != SUCCESS) WRONG_PARAMETER_COUNT; All arguments are stored in a zval container, which needs to be pointed to twice.The snippet above tries to retrieve one argument and make it available to us via the parameter pointer. zend_get_parameters_ex() accepts at least two arguments.The first argument is the number of arguments to retrieve, which should match the number of arguments with which the function has been called; this is why it s important to check for correct call syntax.The second argument (and all following arguments) are pointers to pointers to pointers to zvals. (Confusing, isn t it?) All these pointers are required because Zend works internally with **zval; to adjust a local **zval in our function, zend_get_parameters_ex() requires a pointer to it.
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.
Posted in MySQL | No Comments »
December 1st, 2007
Accepting Arguments 317 Now, in the following sections, read on about how to make use of PHP s internals to build powerful extensions. Accepting Arguments One of the most important issues for language extensions is accepting and dealing with data passed via arguments. Most extensions are built to deal with specific input data (or require parameters to perform their specific actions), and function arguments are the only real way to exchange data between the PHP level and the C level. Of course, there s also the possibility of exchanging data using predefined global values (which is also discussed later), but this should be avoided by all means, as it s extremely bad practice.For details,refer to Chapter 1, Development Concepts. PHP doesn t make use of any formal function declarations; this is why call syntax is always completely dynamic and never checked for errors. Checking for correct call syntax is left to the user code. For example, it s possible to call a function using only one argument at one time and four arguments the next time both invocations are syntactically absolutely correct. Determining the Number of Arguments Since PHP doesn t have formal function definitions with support for call syntax checking, and since PHP features variable arguments, sometimes you need to find out with how many arguments your function has been called.You can use the ZEND_NUM_ARGS macro in this case. In previous versions of PHP, this macro retrieved the number of arguments with which the function has been called based on the function s hash table entry, ht, which is passed in the INTERNAL_FUNCTION_PARAMETERS list.As ht itself now contains the number of arguments that have been passed to the function, ZEND_NUM_ARGS has been stripped down to a dummy macro (see its definition in zend_API.h). But it s still good practice to use it, to remain compatible with future changes in the call interface. Note: The old PHP equivalent of this macro is ARG_COUNT. The following code checks for the correct number of arguments: if(ZEND_NUM_ARGS() != 2) WRONG_PARAMETER_COUNT; If the function is not called with two arguments,it exits with an error message.The code snippet above makes use of the tool macro WRONG_PARAMETER_COUNT, which can be used to generate a standard error message (see Figure 9.4).
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.
Posted in MySQL | No Comments »
November 30th, 2007
316 Chapter 9 Extending PHP 4.0: Hacking the Core of PHP The function implementation is surrounded by a conditional compilation statement. This is needed since the function get_module() is only required if your module is built as a dynamic extension. By specifying a definition of COMPILE_DL in the compiler command (see above for a discussion of the compilation instructions required to build a dynamic extension), you can instruct your module whether you intend to build it as a dynamic extension or as a built-in module. If you want a built-in module, the implementation of get_module() is simply left out. get_module() is called by Zend at load time of the module.You can think of it as being invoked by the dl() call in your script. Its purpose is to pass the module information block back to Zend in order to inform the engine about the module contents. If you don t implement a get_module() function in your dynamic loadable module, Zend will compliment you with an error message when trying to access it. Implementation of All Exported Functions Implementing the exported functions is the final step.The example function in first_module looks like this: ZEND_FUNCTION(firstmodule) { zval **parameter; if((ZEND_NUM_ARGS() != 1) || (zend_get_parameters_ex(1, ¶meter) .!= SUCCESS)) { WRONG_PARAM_COUNT; } convert_to_long_ex(parameter); RETURN_LONG((*parameter)->value.lval); } The function declaration is done using ZEND_FUNCTION, which corresponds to ZEND_FE in the function entry table (discussed earlier). After the declaration, code for checking and retrieving the function s arguments, argument conversion, and return value generation follows (more on this later). Summary That s it, basically there s nothing more to implementing PHP modules. Built-in modules are structured similarly to dynamic modules, so, equipped with the information presented in the previous sections, you ll be able to fight the odds when encountering PHP module source files.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.
Posted in MySQL | No Comments »