1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297616 $ -->
<sect1 xml:id="internals2.structure.lifecycle" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Life cycle of an extension</title>
<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.
</simpara>
<sect2 xml:id="internals2.structure.lifecycle.mod-vs-req">
<title>Loading, unloading, and requests</title>
<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.
</simpara>
</sect2>
<sect2 xml:id="internals2.structure.lifecycle.overview">
<title>Overview</title>
<itemizedlist>
<listitem>
<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
<constant>zend_module</constant> structure as it is loaded.
</simpara>
</listitem>
<listitem>
<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 <constant>zend_module</constant> structure.
</simpara>
</listitem>
<listitem>
<simpara>
Whenever the associated PHP process is done handling a request, each
module receives a call to the "request termination" function specified in
its <constant>zend_module</constant> structure.
</simpara>
</listitem>
<listitem>
<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
<constant>zend_module</constant> structure at this time.
</simpara>
</listitem>
</itemizedlist>
</sect2>
<sect2 xml:id="internals2.structure.lifecycle.what-when">
<title>What to do, and when to do it</title>
<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.
</simpara>
<table>
<title>What to do, and when to do it</title>
<tgroup cols="2">
<thead>
<row>
<entry>Module initialization/termination</entry>
<entry>Request initialization/termination</entry>
</row>
</thead>
<tbody>
<row>
<entry>Allocate/deallocate and initialize module global variables</entry>
<entry>
Allocate/deallocate and initialize request-specific variables
</entry>
</row>
<row>
<entry>Register/unregister class entries</entry>
<entry></entry>
</row>
<row>
<entry>Register/unregister INI entries</entry>
<entry></entry>
</row>
<row>
<entry>Register constants</entry>
<entry></entry>
</row>
</tbody>
</tgroup>
</table>
</sect2>
<sect2 xml:id="internals2.structure.lifecycle.info">
<title>The <function>phpinfo</function> callback</title>
<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
<function>phpinfo</function>. 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.
</simpara>
<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:
</simpara>
<variablelist>
<varlistentry>
<term><function>php_info_print_table_start</function></term>
<listitem>
<simpara>
Open a table in <function>phpinfo</function> output. Takes no parameters.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><function>php_info_print_table_header</function></term>
<listitem>
<simpara>
Print a table header in <function>phpinfo</function> output. Takes one
parameter, the number of columns, plus the same number of
<type>char *</type> parameters which are the texts for each column
heading.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><function>php_info_print_table_row</function></term>
<listitem>
<simpara>
Print a table row in <function>phpinfo</function> output. Takes one
parameter, the number of columns, plus the same number of
<type>char *</type> parameters which are the texts for each column
content.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><function>php_info_print_table_end</function></term>
<listitem>
<simpara>
Close a table formerly opened by
<function>php_info_print_table_start</function>. Takes no parameters.
</simpara>
</listitem>
</varlistentry>
</variablelist>
<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:
</simpara>
<example xml:id="internals2.structure.lifecycle.info.counter">
<title>counter's PHP_MINFO function</title>
<programlisting role="c">
<![CDATA[
/* {{{ 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();
}
/* }}} */
]]>
</programlisting>
</example>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|