📄 internals2.structure.modstruct.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>The zend_module structure</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="internals2.structure.basics.html">Basic constructs</a></div> <div class="next" style="text-align: right; float: right;"><a href="internals2.structure.globals.html">Extension globals</a></div> <div class="up"><a href="internals2.structure.html">Extension structure</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="internals2.structure.modstruct" class="sect1"> <h2 class="title">The zend_module structure</h2> <p class="para"> The main source file of a PHP extension contains several new constructs for a C programmer. The most important of these, the one touched first when starting a new extension, is the <i>zend_module</i> structure. This structure contains a wealth of information that tells the Zend Engine about the extension's dependencies, version, callbacks, and other critical data. The structure has mutated considerably over time; this section will focus on the structure as it has appeared since PHP 5.2, and will identify the very few parts which have changed in PHP 5.3. </p> <p class="para"> The <i>zend_module</i> declaration from <var class="filename">counter.c</var> looks like this before any code has been written. The example file was generated by <strong class="command">ext_skel --extname=counter</strong>, with some obsolete constructs removed: </p> <div class="example" id="internals2.structure.modstruct.example-decl" name="internals2.structure.modstruct.example-decl"> <p><b>Example #1 zend_module declaration in the counter extension</b></p> <div class="example-contents"><div class="cdata"><pre>/* {{{ counter_module_entry */zend_module_entry counter_module_entry = { STANDARD_MODULE_HEADER, "counter", counter_functions, PHP_MINIT(counter), PHP_MSHUTDOWN(counter), PHP_RINIT(counter), /* Replace with NULL if there's nothing to do at request start */ PHP_RSHUTDOWN(counter), /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(counter), "0.1", /* Replace with version number for your extension */ STANDARD_MODULE_PROPERTIES};/* }}} */</pre></div> </div> </div> <p class="para"> This may look a bit daunting at first glance, but most of it is very simple to understand. Here's the declaration of <i>zend_module</i> from <var class="filename">zend_modules.h</var> in PHP 5.3: </p> <div class="example" id="internals2.structure.modstruct.struct-defn" name="internals2.structure.modstruct.struct-defn"> <p><b>Example #2 zend_module definition in PHP 5.3</b></p> <div class="example-contents"><div class="cdata"><pre>struct _zend_module_entry { unsigned short size; unsigned int zend_api; unsigned char zend_debug; unsigned char zts; const struct _zend_ini_entry *ini_entry; const struct _zend_module_dep *deps; const char *name; const struct _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); const char *version; size_t globals_size;#ifdef ZTS ts_rsrc_id* globals_id_ptr;#else void* globals_ptr;#endif void (*globals_ctor)(void *global TSRMLS_DC); void (*globals_dtor)(void *global TSRMLS_DC); int (*post_deactivate_func)(void); int module_started; unsigned char type; void *handle; int module_number;};</pre></div> </div> </div> <p class="para"> Many of these fields will never be touched by an extension writer. There are a number of standard macros that set them to their proper values automatically. The macro <b><tt>STANDARD_MODULE_HEADER</tt></b> fills in everything up to the <var class="varname">deps</var> field. Alternatively, the <b><tt>STANDARD_MODULE_HEADER_EX</tt></b> will leave the <var class="varname">deps</var> field empty for the developer's use. The developer is always responsible for everything from <var class="varname">name</var> to <var class="varname">version</var>. After that, the <b><tt>STANDARD_MODULE_PROPERTIES</tt></b> macro will fill in the rest of the structure, or the <b><tt>STANDARD_MODULE_PROPERTIES_EX</tt></b> macro can be used to leave the extension globals and post-deactivation function fields unfilled. Most modern extensions will make use of module globals. </p> <blockquote><p><b class="note">Note</b>: This table gives the values that each field would have if the developer were to fill in the structure entirely by hand, without recourse to any of the shortcut macros. <em class="emphasis">This is not recommended.</em> The "correct" values for many fields may change. Use the macros whenever possible. <br /> </p></blockquote> <table border="5"> <caption><b>Module structure field values</b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Field</th> <th colspan="1">Value</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">size</var> <a href="#fnidinternals2.structure.modstruct.struct-values.not-for-dev" name="fninternals2.structure.modstruct.struct-values.not-for-dev"><sup>[1]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMHE" name="fninternals2.structure.modstruct.struct-values.given-by-SMHE"><sup>[2]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMH" name="fninternals2.structure.modstruct.struct-values.given-by-SMH"><sup>[3]</sup></a> </td> <td colspan="1" rowspan="1" align="left"><code class="code">sizeof(zend_module_entry)</code></td> <td colspan="1" rowspan="1" align="left"> The size in bytes of the structure. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">zend_api</var> <a href="#fnidinternals2.structure.modstruct.struct-values.not-for-dev"><sup>[1]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMHE"><sup>[2]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMH"><sup>[3]</sup></a> </td> <td colspan="1" rowspan="1" align="left"><b><tt>ZEND_MODULE_API_NO</tt></b></td> <td colspan="1" rowspan="1" align="left"> The version of the Zend API this module was compiled against. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">zend_debug</var> <a href="#fnidinternals2.structure.modstruct.struct-values.not-for-dev"><sup>[1]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMHE"><sup>[2]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMH"><sup>[3]</sup></a> </td> <td colspan="1" rowspan="1" align="left"><b><tt>ZEND_DEBUG</tt></b></td> <td colspan="1" rowspan="1" align="left"> A flag indicating whether the module was compiled with debugging turned on. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">zts</var> <a href="#fnidinternals2.structure.modstruct.struct-values.not-for-dev"><sup>[1]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMHE"><sup>[2]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMH"><sup>[3]</sup></a> </td> <td colspan="1" rowspan="1" align="left"><b><tt>USING_ZTS</tt></b></td> <td colspan="1" rowspan="1" align="left"> A flag indicating whether the module was compiled with ZTS (TSRM) enabled (see <a href="internals2.memory.html" class="xref">Memory management</a>). </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">ini_entry</var> <a href="#fnidinternals2.structure.modstruct.struct-values.not-for-dev"><sup>[1]</sup></a> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMH"><sup>[3]</sup></a> </td> <td colspan="1" rowspan="1" align="left"><b><tt>NULL</tt></b></td> <td colspan="1" rowspan="1" align="left"> This pointer is used internally by Zend to keep a non-local reference to any INI entries declared for the module. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">deps</var> <a href="#fnidinternals2.structure.modstruct.struct-values.given-by-SMH"><sup>[3]</sup></a> </td> <td colspan="1" rowspan="1" align="left"><b><tt>NULL</tt></b></td> <td colspan="1" rowspan="1" align="left"> A pointer to a list of dependencies for the module. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">name</var> </td> <td colspan="1" rowspan="1" align="left">"mymodule"</td> <td colspan="1" rowspan="1" align="left"> The name of the module. This is the short name, such as "spl" or "standard". </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">functions</var> </td> <td colspan="1" rowspan="1" align="left">mymodule_functions</td> <td colspan="1" rowspan="1" align="left"> A pointer to the module's function table, which Zend uses to expose functions in the module to user space. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">module_startup_func</var> </td> <td colspan="1" rowspan="1" align="left">PHP_MINIT(mymodule)</td> <td colspan="1" rowspan="1" align="left"> A callback function that Zend will call the first time a module is loaded into a particular instance of PHP. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">module_shutdown_func</var> </td> <td colspan="1" rowspan="1" align="left">PHP_MSHUTDOWN(mymodule)</td> <td colspan="1" rowspan="1" align="left"> A callback function that Zend will call the when a module is unloaded from a particular instance of PHP, typically during final shutdown. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">request_startup_func</var> </td> <td colspan="1" rowspan="1" align="left">PHP_RINIT(mymodule)</td> <td colspan="1" rowspan="1" align="left"> A callback function that Zend will call at the beginning of each request. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">request_shutdown_func</var> </td> <td colspan="1" rowspan="1" align="left">PHP_RSHUTDOWN(mymodule)</td> <td colspan="1" rowspan="1" align="left"> A callback function that Zend will call at the end of each request. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">info_func</var> </td> <td colspan="1" rowspan="1" align="left">PHP_MINFO(mymodule)</td> <td colspan="1" rowspan="1" align="left"> A callback function that Zend will call when the <a href="function.phpinfo.html" class="function">phpinfo()</a> function is called. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"> <var class="varname">version</var> </td>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -