Encapsulate process-oriented C or C ++ interfaces for PHP. This refers to process-oriented encapsulation.
The purpose of encapsulating PHP extensions is: Because PHP cannot directly use C functions, it needs to use its own memory management and parameter management methods. Therefore, between C code and PHP code, the functions and variables to be used are mapped one by one ".
Encapsulation is roughly divided into six steps:
1. include header files
2. Declare the export Function
3. Declare the Zend Function Block
4. Declare the Zend Module
5. Implement the get_module () function
6. Export Functions
Let's take a look at the simplest example. The source files are test. h and test. cpp. They generate extern_test.so. Then, the dynamic link library is dynamically loaded by test. php.
**************************************** *****************
// Test. h
# Ifndef _ extern_test_h _
# DEFINE _ extern_test_h _
# Include <stdio. h>
// Include the header file, that is, introduce the required macros and API definitions.
# Ifdef _ cplusplus
Extern "C "{
# Endif
# Include "php. H"
# Include "php_ini.h"
# Include "ext/standard/info. H"
# Ifdef _ cplusplus
}
# Endif
# Define extern_test_verstr "0.1.0"
Zend_minfo_function (extern_test );
Zend_function (extern_test_func); // declare the export Function
# Endif
**************************************** ********************
// Test. cpp
# Include "test. H"
// Declare the Zend Function Block
Zend_function_entry extern_test_functions [] =
{
Zend_fe (extern_test_func, null)
{Null, null, null}
};
// Declare the Zend Module
Zend_module_entry extern_test_module_entry =
{
Standard_module_header,
"Extern_test ",
Extern_test_functions,
Null,
Null,
Null,
Null,
Zend_minfo (extern_test ),
Extern_test_verstr,
Standard_module_properties
};
// Implement the get_module () function
# Ifdef _ cplusplus
Begin_extern_c ()
# Endif
Zend_get_module (extern_test)
# Ifdef _ cplusplus
End_extern_c ()
# Endif
Zend_minfo_function (extern_test)
{
}
// Implement the export Function
Zend_function (extern_test_func)
{
Int iint = 0;
If (zend_num_args () = 1 ){
If (zend_parse_parameters (zend_num_args () tsrmls_cc, "L", & iint) = failure ){
Return_long (err_param_num );
}
} Else {
Return_long (err_param_num );
}
Return_long (iint );
}
**************************************** ********************
<? PHP
// Test. php
DL ("extern_test.so ");
Echo extern_test_func (12345 );
?>
**************************************** ********************
After the above PHP program is executed, "12345" will be output"