Source of: /manual/en/internals2.structure.lifecycle.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/internals2.structure.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'internals2.structure.lifecycle.php',
1 => 'Life cycle of an extension',
),
'up' =>
array (
0 => 'internals2.structure.php',
1 => 'Extension structure',
),
'prev' =>
array (
0 => 'internals2.structure.globals.php',
1 => 'Extension globals',
),
'next' =>
array (
0 => 'internals2.structure.tests.php',
1 => 'Testing an extension',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="internals2.structure.lifecycle" class="sect1">
<h2 class="title">Life cycle of an extension</h2>
<p class="simpara">
A Zend 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 id="internals2.structure.lifecycle.mod-vs-req" class="sect2">
<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 Zend
extension may thus see many requests in its lifetime.
</p>
</div>
<div id="internals2.structure.lifecycle.overview" class="sect2">
<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
<b><tt class="constant">zend_module</tt></b> 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 <b><tt class="constant">zend_module</tt></b> 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 <b><tt class="constant">zend_module</tt></b> 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
<b><tt class="constant">zend_module</tt></b> structure at this time.
</span>
</li>
</ul>
</div>
<div id="internals2.structure.lifecycle.what-when" class="sect2">
<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><b>What to do, and when to do it</b></caption>
<thead valign="middle">
<tr valign="middle">
<th>Module initialization/termination</th>
<th>Request initialization/termination</th>
</tr>
</thead>
<tbody valign="middle" class="tbody">
<tr valign="middle">
<td align="left">Allocate/deallocate and initialize module global variables</td>
<td align="left">
Allocate/deallocate and initialize request-specific variables
</td>
</tr>
<tr valign="middle">
<td align="left">Register/unregister class entries</td>
<td class="empty"> </td>
</tr>
<tr valign="middle">
<td align="left">Register/unregister INI entries</td>
<td class="empty"> </td>
</tr>
<tr valign="middle">
<td align="left">Register constants</td>
<td class="empty"> </td>
</tr>
</tbody>
</table>
</div>
<div id="internals2.structure.lifecycle.info" class="sect2">
<h3 class="title">The <a href="function.phpinfo.php" class="function">phpinfo()</a> 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
<a href="function.phpinfo.php" class="function">phpinfo()</a>. 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 class="varlistentry">
<span class="term"><b>php_info_print_table_start()</b></span>
</dt><dd class="listitem">
<span class="simpara">
Open a table in <a href="function.phpinfo.php" class="function">phpinfo()</a> output. Takes no parameters.
</span>
</dd>
<dt class="varlistentry">
<span class="term"><b>php_info_print_table_header()</b></span>
</dt><dd class="listitem">
<span class="simpara">
Print a table header in <a href="function.phpinfo.php" class="function">phpinfo()</a> output. Takes one
parameter, the number of columns, plus the same number of
<span class="type char *">char *</span> parameters which are the texts for each column
heading.
</span>
</dd>
<dt class="varlistentry">
<span class="term"><b>php_info_print_table_row()</b></span>
</dt><dd class="listitem">
<span class="simpara">
Print a table row in <a href="function.phpinfo.php" class="function">phpinfo()</a> output. Takes one
parameter, the number of columns, plus the same number of
<span class="type char *">char *</span> parameters which are the texts for each column
content.
</span>
</dd>
<dt class="varlistentry">
<span class="term"><b>php_info_print_table_end()</b></span>
</dt><dd class="listitem">
<span class="simpara">
Close a table formerly opened by
<b>php_info_print_table_start()</b>. Takes no parameters.
</span>
</dd>
</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">
<p><b>Example #1 counter's PHP_MINFO function</b></p>
<div class="example-contents programlisting">
<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><?php manual_footer(); ?>