Skip to content

Commit c748daa

Browse files
author
Julien Pauli
committed
reworked chapters and codestyle
1 parent 4310ccc commit c748daa

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

Book/php7/extensions_design/php_functions.rst

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@ If you have read :doc:`the chapter about tests <../tests>`, you should now write
366366

367367
\... and launch ``make test``
368368

369-
Advanced example
370-
****************
369+
Playing with constants
370+
**********************
371371

372-
Let's go in some advance examples.
372+
Let's go with an advanced example.
373373
Let's add the opposite function: ``celsius_to_fahrenheit($celsius)``::
374374

375375
ZEND_BEGIN_ARG_INFO_EX(arginfo_celsius_to_fahrenheit, 0, 0, 1)
@@ -378,25 +378,25 @@ Let's add the opposite function: ``celsius_to_fahrenheit($celsius)``::
378378

379379
static double php_celsius_to_fahrenheit(double c)
380380
{
381-
return (((double)9/5) * c) + 32 ;
381+
return (((double)9/5) * c) + 32 ;
382382
}
383383

384384
PHP_FUNCTION(celsius_to_fahrenheit)
385385
{
386-
double c;
386+
double c;
387387

388-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "d", &c) == FAILURE) {
389-
return;
390-
}
388+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "d", &c) == FAILURE) {
389+
return;
390+
}
391391

392-
RETURN_DOUBLE(php_celsius_to_fahrenheit(c));
392+
RETURN_DOUBLE(php_celsius_to_fahrenheit(c));
393393
}
394394

395395
static const zend_function_entry pib_functions[] =
396396
{
397-
PHP_FE(fahrenheit_to_celsius, arginfo_fahrenheit_to_celsius) /* Done above */
398-
PHP_FE(celsius_to_fahrenheit,arginfo_celsius_to_fahrenheit) /* just added */
399-
PHP_FE_END
397+
PHP_FE(fahrenheit_to_celsius, arginfo_fahrenheit_to_celsius) /* Done above */
398+
PHP_FE(celsius_to_fahrenheit,arginfo_celsius_to_fahrenheit) /* just added */
399+
PHP_FE_END
400400
};
401401
402402
Now a more complex use case, we show it in PHP before implementing it as a C extension:
@@ -431,10 +431,10 @@ constants against the engine.
431431
Here is a constant, internally, a ``zend_constant`` structure::
432432

433433
typedef struct _zend_constant {
434-
zval value;
435-
zend_string *name;
436-
int flags;
437-
int module_number;
434+
zval value;
435+
zend_string *name;
436+
int flags;
437+
int module_number;
438438
} zend_constant;
439439

440440
Really an easy structure (that could become a nightmare if you deeply look at how constants are managed into the
@@ -448,10 +448,10 @@ To register constants, here again there is no difficulty at all, a bunch of macr
448448

449449
PHP_MINIT_FUNCTION(pib)
450450
{
451-
REGISTER_LONG_CONSTANT("TEMP_CONVERTER_TO_CELSIUS", TEMP_CONVERTER_TO_CELSIUS, CONST_CS|CONST_PERSISTENT);
452-
REGISTER_LONG_CONSTANT("TEMP_CONVERTER_TO_FAHRENHEIT", TEMP_CONVERTER_TO_FAHRENHEIT, CONST_CS|CONST_PERSISTENT);
451+
REGISTER_LONG_CONSTANT("TEMP_CONVERTER_TO_CELSIUS", TEMP_CONVERTER_TO_CELSIUS, CONST_CS|CONST_PERSISTENT);
452+
REGISTER_LONG_CONSTANT("TEMP_CONVERTER_TO_FAHRENHEIT", TEMP_CONVERTER_TO_FAHRENHEIT, CONST_CS|CONST_PERSISTENT);
453453

454-
return SUCCESS;
454+
return SUCCESS;
455455
}
456456

457457
.. note:: It is a good practice to give PHP constants values of C macros. That ease things, and that's what we did.
@@ -478,34 +478,34 @@ Then we add our new function to the function registration vector::
478478

479479
static const zend_function_entry pib_functions[] =
480480
{
481-
PHP_FE(fahrenheit_to_celsius,arginfo_fahrenheit_to_celsius) /* seen above */
482-
PHP_FE(celsius_to_fahrenheit,arginfo_celsius_to_fahrenheit) /* seen above */
483-
PHP_FE(temperature_converter, arginfo_temperature_converter) /* our new function */
481+
PHP_FE(fahrenheit_to_celsius,arginfo_fahrenheit_to_celsius) /* seen above */
482+
PHP_FE(celsius_to_fahrenheit,arginfo_celsius_to_fahrenheit) /* seen above */
483+
PHP_FE(temperature_converter, arginfo_temperature_converter) /* our new function */
484484
}
485485

486486
And, the function body::
487487

488488
PHP_FUNCTION(temperature_converter)
489489
{
490-
double t;
491-
zend_long mode = TEMP_CONVERTER_TO_CELSIUS;
492-
zend_string *result;
490+
double t;
491+
zend_long mode = TEMP_CONVERTER_TO_CELSIUS;
492+
zend_string *result;
493493

494-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "d|l", &t, &mode) == FAILURE) {
495-
return;
496-
}
494+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "d|l", &t, &mode) == FAILURE) {
495+
return;
496+
}
497497

498-
switch (mode)
499-
{
500-
case TEMP_CONVERTER_TO_CELSIUS:
501-
result = strpprintf(0, "%.2f degrees fahrenheit gives %.2f degrees celsius", t, php_fahrenheit_to_celsius(t));
502-
RETURN_STR(result);
503-
case TEMP_CONVERTER_TO_FAHRENHEIT:
504-
result = strpprintf(0, "%.2f degrees celsius gives %.2f degrees fahrenheit", t, php_celsius_to_fahrenheit(t));
505-
RETURN_STR(result);
506-
default:
507-
php_error(E_WARNING, "Invalid mode provided, accepted values are 1 or 2");
508-
}
498+
switch (mode)
499+
{
500+
case TEMP_CONVERTER_TO_CELSIUS:
501+
result = strpprintf(0, "%.2f degrees fahrenheit gives %.2f degrees celsius", t, php_fahrenheit_to_celsius(t));
502+
RETURN_STR(result);
503+
case TEMP_CONVERTER_TO_FAHRENHEIT:
504+
result = strpprintf(0, "%.2f degrees celsius gives %.2f degrees fahrenheit", t, php_celsius_to_fahrenheit(t));
505+
RETURN_STR(result);
506+
default:
507+
php_error(E_WARNING, "Invalid mode provided, accepted values are 1 or 2");
508+
}
509509
}
510510

511511
Remember to well look at `README.PARAMETER_PARSING_API <https://github.com/php/php-src/blob/
@@ -523,6 +523,9 @@ the ``return_value`` zval using ``RETURN_STR()``.
523523
.. note:: ``strpprintf()`` and its sisters are explained in
524524
:doc:`the chapter about printing functions <../internal_types/strings/printing_functions>`.
525525

526+
A go with Hashtables (PHP arrays)
527+
*********************************
528+
526529
Let's go now for a play with *PHP arrays* and design:
527530

528531
.. code-block:: php
@@ -546,30 +549,30 @@ make the maths operations and add the result in ``return_value``, as an array::
546549
static const zend_function_entry pib_functions[] =
547550
{
548551
/* ... */
549-
PHP_FE(multiple_fahrenheit_to_celsius, arginfo_multiple_fahrenheit_to_celsius)
550-
PHP_FE_END
552+
PHP_FE(multiple_fahrenheit_to_celsius, arginfo_multiple_fahrenheit_to_celsius)
553+
PHP_FE_END
551554
};
552555

553556
PHP_FUNCTION(multiple_fahrenheit_to_celsius)
554557
{
555-
HashTable *temperatures;
556-
zval *data;
558+
HashTable *temperatures;
559+
zval *data;
557560

558-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &temperatures) == FAILURE) {
559-
return;
560-
}
561-
if (zend_hash_num_elements(temperatures) == 0) {
562-
return;
563-
}
561+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &temperatures) == FAILURE) {
562+
return;
563+
}
564+
if (zend_hash_num_elements(temperatures) == 0) {
565+
return;
566+
}
564567

565-
array_init_size(return_value, zend_hash_num_elements(temperatures));
568+
array_init_size(return_value, zend_hash_num_elements(temperatures));
566569

567-
ZEND_HASH_FOREACH_VAL(temperatures, data)
568-
zval dup;
569-
ZVAL_COPY_VALUE(&dup, data);
570-
convert_to_double(&dup);
571-
add_next_index_double(return_value, php_fahrenheit_to_celsius(Z_DVAL(dup)));
572-
ZEND_HASH_FOREACH_END();
570+
ZEND_HASH_FOREACH_VAL(temperatures, data)
571+
zval dup;
572+
ZVAL_COPY_VALUE(&dup, data);
573+
convert_to_double(&dup);
574+
add_next_index_double(return_value, php_fahrenheit_to_celsius(Z_DVAL(dup)));
575+
ZEND_HASH_FOREACH_END();
573576
}
574577

575578

0 commit comments

Comments
 (0)