<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Life cycle of an extension</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="internals2.structure.globals.html">Extension globals</a></div> <div class="next" style="text-align: right; float: right;"><a href="internals2.structure.tests.html">Testing an extension</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.lifecycle" class="sect1"> <h2 class="title">Life cycle of an extension</h2> <p class="simpara"> A PHP extension goes through several phases during its lifetime. All of these phases are opportunities for the developer to perform various initialization, termination, or informational functions. The Zend API allows for hooks into five separate phases of an extension's existence, apart from calls by PHP functions. </p> <div class="sect2" id="internals2.structure.lifecycle.mod-vs-req"> <h3 class="title">Loading, unloading, and requests</h3> <p class="simpara"> As the Zend engine runs, it processes one or more "requests" from its client. In the traditional CGI implementation, this corresponds to one execution of a process. However, many other implementations, most notably the Apache module, can map many requests onto a single PHP process. A PHP extension may thus see many requests in its lifetime. </p> </div> <div class="sect2" id="internals2.structure.lifecycle.overview"> <h3 class="title">Overview</h3> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> In the Zend API, a module is loaded into memory only once when the associated PHP process starts up. Each module receives a call to the "module initialization" function specified in its <strong><code>zend_module</code></strong> structure as it is loaded. </span> </li> <li class="listitem"> <span class="simpara"> Whenever the associated PHP process starts to handle a request from its client - i.e. whenever the PHP interpreter is told to start working - each module receives a call to the "request initialization" function specified in its <strong><code>zend_module</code></strong> structure. </span> </li> <li class="listitem"> <span class="simpara"> Whenever the associated PHP process is done handling a request, each module receives a call to the "request termination" function specified in its <strong><code>zend_module</code></strong> structure. </span> </li> <li class="listitem"> <span class="simpara"> A given module is unloaded from memory when its associated PHP process is shut down in an orderly manner. The module receives a call to the "module termination" function specified in its <strong><code>zend_module</code></strong> structure at this time. </span> </li> </ul> </div> <div class="sect2" id="internals2.structure.lifecycle.what-when"> <h3 class="title">What to do, and when to do it</h3> <p class="simpara"> There are many tasks that might be performed at any of these four points. This table details where many common initialization and termination tasks belong. </p> <table class="doctable table"> <caption><strong>What to do, and when to do it</strong></caption> <thead> <tr> <th>Module initialization/termination</th> <th>Request initialization/termination</th> </tr> </thead> <tbody class="tbody"> <tr> <td>Allocate/deallocate and initialize module global variables</td> <td> Allocate/deallocate and initialize request-specific variables </td> </tr> <tr> <td>Register/unregister class entries</td> <td class="empty"> </td> </tr> <tr> <td>Register/unregister INI entries</td> <td class="empty"> </td> </tr> <tr> <td>Register constants</td> <td class="empty"> </td> </tr> </tbody> </table> </div> <div class="sect2" id="internals2.structure.lifecycle.info"> <h3 class="title">The <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> callback</h3> <p class="simpara"> Aside from globals initialization and certain rarely-used callbacks, there is one more part of a module's lifecycle to examine: A call to <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span>. The output a user sees from this call, whether text or HTML or anything else, is generated by each individual extension that is loaded into the PHP interpreter at the time the call is made. </p> <p class="simpara"> To provide for format-neutral output, the header "ext/standard/info.h" provides an array of functions to produce standardized display elements. Specifically, several functions which create the familiar tables exist: </p> <dl> <dt> <span class="term"> <span class="function"><strong>php_info_print_table_start()</strong></span></span> <dd> <span class="simpara"> Open a table in <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> output. Takes no parameters. </span> </dd> </dt> <dt> <span class="term"> <span class="function"><strong>php_info_print_table_header()</strong></span></span> <dd> <span class="simpara"> Print a table header in <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> output. Takes one parameter, the number of columns, plus the same number of <span class="type"><span class="type char *">char *</span></span> parameters which are the texts for each column heading. </span> </dd> </dt> <dt> <span class="term"> <span class="function"><strong>php_info_print_table_row()</strong></span></span> <dd> <span class="simpara"> Print a table row in <span class="function"><a href="function.phpinfo.html" class="function">phpinfo()</a></span> output. Takes one parameter, the number of columns, plus the same number of <span class="type"><span class="type char *">char *</span></span> parameters which are the texts for each column content. </span> </dd> </dt> <dt> <span class="term"> <span class="function"><strong>php_info_print_table_end()</strong></span></span> <dd> <span class="simpara"> Close a table formerly opened by <span class="function"><strong>php_info_print_table_start()</strong></span>. Takes no parameters. </span> </dd> </dt> </dl> <p class="simpara"> Using these four functions, it is possible to produce status information for nearly any combination of features in an extension. Here is the information callback from the counter extension: </p> <div class="example" id="internals2.structure.lifecycle.info.counter"> <p><strong>Example #1 counter's PHP_MINFO function</strong></p> <div class="example-contents"> <div class="ccode"><pre class="ccode">/* {{{ PHP_MINFO(counter) */ PHP_MINFO_FUNCTION(counter) { char buf[10]; php_info_print_table_start(); php_info_print_table_row(2, "counter support", "enabled"); snprintf(buf, sizeof(buf), "%ld", COUNTER_G(basic_counter_value)); php_info_print_table_row(2, "Basic counter value", buf); php_info_print_table_end(); } /* }}} */</pre> </div> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="internals2.structure.globals.html">Extension globals</a></div> <div class="next" style="text-align: right; float: right;"><a href="internals2.structure.tests.html">Testing an extension</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></body></html>