| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Accessing ArgumentsTo 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. Obrßzek 32-3. Table 9.4. Argument Conversion Functions
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:
Obrßzek 32-5. Listing 9.8. PHP/Zend zval type definition.
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. Obrßzek 32-6. Table 9.5. Zend zval Structure
Obrßzek 32-7. Table 9.6. Zend zvalue_value Structure
Obrßzek 32-8. Table 9.7. Zend Variable Type Constants
To access a long you access zval.value.lval, to access a double you use zval.value.dval, and so on. Because all values are stored in a union, trying to access data with incorrect union members results in meaningless output. Accessing arrays and objects is a bit more complicated and is discussed later. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|