[PHP] macro definition in common PHP source code, source code
Macro definition of common PHP source code:
# Define macro name string
# Indicates that this is a preprocessing command. All preprocessing commands start. Define is a pre-processing command. Macro names are one type of identifiers, with the same naming rules and identifiers. A string can be a constant or expression.
During compilation and preprocessing, all the "macro names" in the program are replaced by the strings in the macro definition. This is called "macro replacement" or "macro expansion ".
1. # define PHP_FUNCTION ZEND_FUNCTION
In the compilation and preprocessing phase, PHP_FUNCTION is replaced with ZEND_FUNCTION.
Macro definition with parameters:
# Define macro name (parameter list) String
Allow macros with parameters. In macro definition, parameters are called formal parameters, and in macro calls, parameters are called actual parameters, which are similar to functions.
1. # define ZEND_FUNCTION (name) ZEND_NAMED_FUNCTION (ZEND_FN (name ))
ZEND_FUNCTION (count) is replaced with ZEND_NAMED_FUNCTION (ZEND_FN (count ))
The concatenation of macro parameters and macro parameters:
Use # In macro definition to convert macro parameters to strings, that is, add quotation marks at the beginning and end of macro parameters.
# A connector used to connect macro parameters or other strings.
1. # define ZEND_FN (name) zif _ # name
If the macro parameter is set to a connector, ZEND_FN (count) is replaced with zif_count.
Multi-expression macro definition:
The do {} while (0) statement format is used in the macro definition, and the code in it will be executed at least once without damaging the original structure.
Source code summary:
Php source code
# Define PHP_FUNCTION ZEND_FUNCTION
# Define ZEND_FUNCTION (name) ZEND_NAMED_FUNCTION (ZEND_FN (name ))
# Define ZEND_FN (name) zif _ # name
# Define ZEND_NAMED_FUNCTION (name) void name (INTERNAL_FUNCTION_PARAMETERS)
# Define INTERNAL_FUNCTION_PARAMETERS int ht, zval * return_value, zval ** return_value_ptr, zval * this_ptr, int return_value_used TSRMLS_DC
PHP_FUNCTION (count );
After pre-processor processing:
1. ZEND_FUNCTION (count)
2. ZEND_NAMED_FUNCTION (ZEND_FN (count ))
3. ZEND_NAMED_FUNCTION (zif_count)
4. void zif_count (INTERNAL_FUNCTION_PARAMETERS)
5. void zif_count (int ht, zval * return_value, zval ** return_value_ptr, zval * this_ptr, int return_value_used TSRMLS_DC)
Exercise:
# Include <stdio. h >#define N 100 # define M (y) y * y # define STR (s) # s # define CON (a, B) a ## B # define ALLOC_ZVAL (z) do {\ z ++; \ z = z + 100 ;\} while (0) int main () {printf ("% d \ n", N); // normal macro calls printf ("% d \ n", M (N )); // specify the parameter printf ("% s \ n", STR (taoshihan); // string-based printf ("% d \ n", CON (123,456 )); // connector int I = 0; ALLOC_ZVAL (I); // multi-row macro defines printf ("% d \ n", I );}