Skip to content

Commit 599ba56

Browse files
7snovicJulien Pauli
authored andcommitted
mention alternative way for parsing the parameters
1 parent 09c5cc2 commit 599ba56

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

Book/php7/extensions_design/php_functions.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,54 @@ To do that, some ``RETURN_***()`` macros are dedicated as well as some ``RETVAL_
351351
Both just set the type and value of the ``return_value`` zval, but ``RETURN_***()`` ones will follow that by a C
352352
``return`` that will return from that current function.
353353

354+
Alternatively, the API provides alternative macros to handle and parse parameters, It's more readable if you get
355+
messed with the python style specifiers.
356+
357+
You will need to starting & ending parsing the function parameters with the following macros::
358+
359+
ZEND_PARSE_PARAMETERS_START(min_argument_count, max_argument_count) /* takes two parameters */
360+
/* here we will go with argument lists */
361+
ZEND_PARSE_PARAMETERS_END();
362+
363+
The available parameters macros could be listed as follows (more on the required arguments for those macros later)::
364+
365+
Z_PARAM_ARRAY() /* old "a" */
366+
Z_PARAM_ARRAY_OR_OBJECT() /* old "A" */
367+
Z_PARAM_BOOL() /* old "b" */
368+
Z_PARAM_CLASS() /* old "C" */
369+
Z_PARAM_DOUBLE() /* old "d" */
370+
Z_PARAM_FUNC() /* old "f" */
371+
Z_PARAM_ARRAY_HT() /* old "h" */
372+
Z_PARAM_ARRAY_OR_OBJECT_HT() /* old "H" */
373+
Z_PARAM_LONG() /* old "l" */
374+
Z_PARAM_STRICT_LONG() /* old "L" */
375+
Z_PARAM_OBJECT() /* old "o" */
376+
Z_PARAM_OBJECT_OF_CLASS() /* old "O" */
377+
Z_PARAM_PATH() /* old "p" */
378+
Z_PARAM_PATH_STR() /* old "P" */
379+
Z_PARAM_RESOURCE() /* old "r" */
380+
Z_PARAM_STRING() /* old "s" */
381+
Z_PARAM_STR() /* old "S" */
382+
Z_PARAM_ZVAL() /* old "z" */
383+
Z_PARAM_VARIADIC() /* old "+" and "*" */
384+
385+
And to add a parameter as an optional parameter we use the following macro::
386+
387+
Z_PARAM_OPTIONAL /* old "|" */
388+
389+
Here is our example with the new parameters parsing style::
390+
391+
PHP_FUNCTION(fahrenheit_to_celsius)
392+
{
393+
double f;
394+
395+
ZEND_PARSE_PARAMETERS_START(1, 1)
396+
Z_PARAM_DOUBLE(f);
397+
ZEND_PARSE_PARAMETERS_END();
398+
399+
RETURN_DOUBLE(php_fahrenheit_to_celsius(f));
400+
}
401+
354402
Adding tests
355403
************
356404

0 commit comments

Comments
 (0)