05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The value returned from a <strong>PHP</strong> function is returned in a special zval container called<br />

return_value, which is automatically allocated. In the example, we assign return_<br />

value to the passed arg container, call zval_copy_ctor( ) to make a copy, and ensure<br />

that we convert the data to a string.<br />

We also skipped the zval dereferencing convenience macros Z_STRVAL_PP( ) and Z_<br />

STRLEN_PP( ) and instead dereferenced the return_value zval container manually.<br />

Going this low-level is not recommended, however, as changes in the underlying<br />

data structures could break your extension.<br />

Parameter Handling<br />

As we learned in the previous section on the pval/zval container, there are at least<br />

two ways to accept and parse arguments to <strong>PHP</strong> functions you write. We will concentrate<br />

on the higher-level zend_parse_parameters( ) function here.<br />

There are two versions of the function, prototyped like this in C:<br />

int zend_parse_parameters(int num_args TSRMLS_DC, char *type_spec, ...);<br />

int zend_parse_parameters_ex(int flags, int num_args TSRMLS_DC,<br />

char *type_spec, ...);<br />

They differ only in that the ex, or expanded, version of the function contains a flags<br />

parameter. The only flag currently supported is ZEND_PARSE_PARAMS_QUIET, which<br />

inhibits warnings from supplying an incorrect number or type of arguments.<br />

Both parameter-parsing functions return either SUCCESS or FAILURE. The functions<br />

take any number of extra arguments (pointers to variables whose values are assigned<br />

by the parsing function). On failure the return_value of the function is automatically<br />

set to FALSE, so you can simply return from your function on a failure.<br />

The most complex part of these functions is the type_spec string you pass them.<br />

Here’s the relevant part of our rot13 example:<br />

char *arg = NULL;<br />

int arg_len, argc = ZEND_NUM_ARGS( );<br />

if (zend_parse_parameters(argc TSRMLS_CC, "s/", &arg, &arg_len) == FAILURE)<br />

return;<br />

We first get the number of arguments passed to this function by calling the ZEND_NUM_<br />

ARGS( ) macro. We pass this number along with a type_spec string of "s/" and then<br />

the address of a char * and the address of an int. The “s” in the type_spec string indicates<br />

that we are expecting a string argument. For each string argument, the function<br />

fills in the char * and int with the contents of the string and the length of the<br />

string. The “/” character in the type_spec indicates that the string should be separated<br />

from the calling container. We did this in our rot13 example because we<br />

wanted to modify the passed string.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Parameter Handling | 335

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!