PHP extension module structure

Source: Internet
Author: User
Tags zts
All PHP extensions follow a common structure

1. the header file contains (including all required macros and APIs). 2. c. Declare the export function. 3. Declare the Zend function block. I. header file inclusionBy default, a new php_extname.h header file is created through the extension created by ext_seketon. This file contains PHP. H, which imports the basic Zend macros and APIs.

Ii. Declare the export FunctionZend_function (my_function), which provides functions called in PHP. Expand this macro: void zif_my_function (outputs) void zif_my_function (int ht, zval * return_value, zval * this_ptr, int return_value_users, role * executor_globals); Parameter Function:

Parameters Description
HT The number of parameters received. Considering backward compatibility, zend_num_args () is used to replace ht.
Return_value The return value passed to the PHP interface.
This_ptr If this function is a method in the object, this_ptr returns the current object
Return_value_used Indicates whether the function is returned to the PHP interface.
Executor_globals Point to the global settings of the Zend engine.

3. Declare Zend function blocksCreate the zend_function_entry array and provide Zend as the PHP interface.

typedef struct _zend_function_entry {    char *fname;    void (*handler)(INTERNAL_FUNCTION_PARAMETERS);    unsigned char *func_arg_types;} zend_function_entry;
Parameters Description
Fname The name of the function provided to PhP. For example, mysql_connect
Handler The pointer responsible for processing this interface function.
Func_arg_types Mark parameters. It can be set to null.
static const zend_function_entry mysql_functions[] = {                PHP_FE(mysql_connect,  arginfo_mysql_connect)                PHP_FE(mysql_pconnect,  arginfo_mysql_pconnect)                PHP_FE(mysql_close,  arginfo__optional_mysql_link)                PHP_FE(mysql_select_db,  arginfo_mysql_select_db)     {NULL, NULL, NULL}}
The preceding section lists the zend_function_entry defined in the MySQL extension. {Null, null, null} indicates the end of the array.

Iv. Declare the Zend module Extension StructureThis block information must be stored in the zend_module_entry structure and contain necessary information about the module. For example, initialize the module function pointer, Module name, and version information.

struct _zend_module_entry {    unsigned short size;    unsigned int zend_api;    unsigned char zend_debug;    unsigned char zts;    char *name;    zend_function_entry *functions;    int (*module_startup_func)(INIT_FUNC_ARGS);    int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);    int (*request_startup_func)(INIT_FUNC_ARGS);    int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);    void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);    char *version;    [more]};

typedef struct _zend_module_entry zend_module_entry;

Parameters Description
Size, zend_api, zend_debug and ZTS It is usually filled with standard_module_header,
Name Extension name
Functions Pointing to zend_functions_entry pointer
Module_startup_func Function pointer called during module initialization. Used to put some initialization steps. If a fault occurs during initialization, return failure, and return success if successful. Declare An initialization function using zend_minit
Module_shutdown_func The function pointer called when the module is disabled, which is used for one-time analysis steps. Such as releasing resources.
Sucess is returned successfully, failure is returned if the request fails, and null is returned if the request is not used. Declare zend_mshutdown
Request_startup_func Call this function before each request is processed. Successful sucess, failed failure, null is returned if not used. Declare to use zend_rinit.
From the Web, this function is called every request.
Request_startup_func This function is called before and after each request is processed. Successful sucess, failed failure, null is returned if not used. Declare to use zend_rinit.
Request_shutdown_func Call this function after each request is processed. Successful sucess, failed failure, null is returned if not used. Declare zend_rshutdown.
Info_func The information about this extension is printed when phpinfo () is called.
This information is output by this function.
Declare to use zend_minfo
Version The extended string version number. If no version is available, use no_version_yet.
[More] You can use the macro standard_module_properties_ex or standard_module_properties.
zend_module_entry firstmod_module_entry ={    STANDARD_MODULE_HEADER,    "New Module",    firstmod_functions,    NULL, NULL, NULL, NULL, NULL,    NO_VERSION_YET,    STANDARD_MODULE_PROPERTIES,};
This is a basic module structure. The module name is "new module" and the function list is firstmod_functions. the startup and shutdown functions are not set.
The following is a PHP extension file named test. Through the above introduction, we will have a clear understanding of PHP extension development steps by comparing the following code.
# Ifdef have_config_h # include "config. H "# endif/* contains Zend APIs, macros, and basic PHP built-in functions, such as php_trim */# include" php. H "# include" php_ini.h "# include" ext/standard/info. H "# include" php_test.h "/* enable the global variable in the module */zend_declare_module_globals (TEST)/* true global resources-no need for thread safety here */static int le_test; /** declare the Function Array and provide it to PhP using */const zend_function_entry test_functions [] = {php_fe (my_function, null)/* Testing, remove later. */php_fe_end/* must be the last line in test_functions [] */};/*} * // * module structure, declare Startup \ shutdown, Module name, and function */zend_module_entry test_module_entry ={# if zend_module_api_no> = 20010901 standard_module_header, # endif "test", test_functions, php_minit (test), php_mshutdown (test), php_rinit (test),/* replace with null if there's nothing to do at request start */php_rshu Tdown (test),/* replace with null if there's nothing to do at request end */php_minfo (test), # If zend_module_api_no> = 20010901 "0.1 ", /* replace with version number for your extension */# endif standard_module_properties}; # ifdef compile_dl_testzend_get_module (TEST) # endif/* from PHP. read configuration information in the ini configuration file */php_ini_begin () std_php_ini_entry ("test. global_value "," 42 ", php_ini_all, onupdatelong, global_value, ze Nd_test_globals, test_globals) std_php_ini_entry ("test. global_string "," foobar ", php_ini_all, onupdatestring, global_string, zend_test_globals, test_globals) php_ini_end ()/* initialize the default value of the global variable */static void merge) {test_globals-> global_value = 0; test_globals-> global_string = NULL;}/* is called when the module is loaded for the first time */php_minit_function (test) {/* register the global variable */register_ini_entries (); Return success;}/* call */php_mshutdown_function (TEST) {/* release global variable */unregister_ini_entries (); Return success ;} /* call */php_rinit_function (TEST) {return success;}/* call */php_rshutdown_function (TEST) {return success;} before each request ;} /* phpinfo () Output extension information */php_minfo_function (TEST) {php_info_print_table_start (); php_info_print_table_header (2, "test support", "enabled"); php_info_print_table_end (); /* Whether to lose Output PHP. configuration Information in ini display_ini_entries (); */}/* definition function my_function is provided to PhP using */php_function (my_function) {char * Arg = NULL; int arg_len, Len; char * strg; If (zend_parse_parameters (zend_num_args () tsrmls_cc, "S", & Arg, & arg_len) = failure) {return;} Len = spprintf (& strg, 0, "Congratulations! You have successfully modified EXT/%. 78 s/config. m4. module %. 78 s is now compiled into PHP. "," test ", ARG); return_stringl (strg, Len, 0 );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.