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
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 297078 $ -->
<sect2 xml:id="internals2.ze1.zendapi.printing" xmlns="http://docbook.org/ns/docbook">
<title>Printing Information</title>
<para>
Often it's necessary to print messages to the output stream from
your module, just as <function>print</function> would be used
within a script. PHP offers functions for most generic tasks, such
as printing warning messages, generating output for
<function>phpinfo</function>, and so on. The following sections
provide more details. Examples of these functions can be found on
the CD-ROM.
</para>
<sect3 xml:id="internals2.ze1.zendapi.printing.zend-printf">
<title><function>zend_printf</function></title>
<para>
<function>zend_printf</function> works like the
standard <function>printf</function>, except that it prints to Zend's
output stream.
</para>
</sect3>
<sect3 xml:id="internals2.ze1.zendapi.printing.zend-error">
<title><function>zend_error</function></title>
<para>
<function>zend_error</function> can be used to generate error messages.
This function accepts two arguments; the first is the error type (see
<filename>zend_errors.h</filename>), and the second is the error message.
<programlisting role="c">
<![CDATA[
zend_error(E_WARNING, "This function has been called with empty arguments");
]]>
</programlisting>
<xref linkend="internals2.ze1.zendapi.tab.error-messages"/> shows a list
of possible values (see <link
linkend="internals2.ze1.zendapi.fig.warning-messages">below</link>). These
values are also referred to in <filename>php.ini</filename>. Depending on
which error type you choose, your messages will be logged.
<table xml:id="internals2.ze1.zendapi.tab.error-messages">
<title>Zend's Predefined Error Messages.</title>
<tgroup cols="2">
<colspec colnum="1" colname="col1" colwidth="1.00*"/>
<colspec colnum="2" colname="col2" colwidth="1.36*"/>
<tbody>
<row>
<entry colname="col1">Error</entry>
<entry colname="col2">Description</entry>
</row>
<row>
<entry colname="col1"><constant>E_ERROR</constant></entry>
<entry colname="col2">
Signals an error and terminates execution of the script
immediately.</entry>
</row>
<row>
<entry colname="col1"><constant>E_WARNING</constant></entry>
<entry colname="col2">
Signals a generic warning. Execution continues.
</entry>
</row>
<row>
<entry colname="col1"><constant>E_PARSE</constant></entry>
<entry colname="col2">
Signals a parser error. Execution continues.
</entry>
</row>
<row>
<entry colname="col1"><constant>E_NOTICE</constant></entry>
<entry colname="col2">
Signals a notice. Execution continues. Note that by
default the display of this type of error messages is turned off in
<filename>php.ini</filename>.
</entry>
</row>
<row>
<entry colname="col1"><constant>E_CORE_ERROR</constant></entry>
<entry colname="col2">
Internal error by the core; shouldn't be used by
user-written modules.
</entry>
</row>
<row>
<entry colname="col1"><constant>E_COMPILE_ERROR</constant></entry>
<entry colname="col2">
Internal error by the compiler; shouldn't be used by
user-written modules.
</entry>
</row>
<row>
<entry colname="col1"><constant>E_COMPILE_WARNING</constant></entry>
<entry colname="col2">
Internal warning by the compiler; shouldn't be used by
user-written modules.
</entry>
</row>
</tbody>
</tgroup>
</table>
<mediaobject xml:id="internals2.ze1.zendapi.fig.warning-messages">
<alt>Display of warning messages in the browser.</alt>
<imageobject>
<imagedata fileref="en/internals2/ze1/zendapi/figures/zend.07-warning-messages.png"/>
</imageobject>
</mediaobject>
</para>
</sect3>
<sect3 xml:id="internals2.ze1.zendapi.printing.phpinfo">
<title>Including Output in <function>phpinfo</function></title>
<para>
After creating a real module, you'll want to show information
about the module in <function>phpinfo</function> (in addition to the
module name, which appears in the module list by default). PHP allows
you to create your own section in the <function>phpinfo</function> output with the <literal>ZEND_MINFO()</literal> function. This function
should be placed in the module descriptor block (discussed earlier) and is
always called whenever a script calls <function>phpinfo</function>.
</para>
<para>
PHP automatically prints a section
in <function>phpinfo</function> for you if you specify the <literal>ZEND_MINFO</literal>
function, including the module name in the heading. Everything else must be
formatted and printed by you.
</para>
<para>
Typically, you can print an HTML table header
using <function>php_info_print_table_start</function> and then use the standard
functions <function>php_info_print_table_header</function>
and <function>php_info_print_table_row</function>. As arguments, both take the number of
columns (as integers) and the column contents (as strings). <xref linkend="internals2.ze1.zendapi.example.phpinfo"/> shows a source example and its output. To print the table footer, use <function>php_info_print_table_end</function>.
</para>
<example xml:id="internals2.ze1.zendapi.example.phpinfo">
<title>
Source code and screenshot for output in <function>phpinfo</function>.
</title>
<programlisting role="c">
<![CDATA[
php_info_print_table_start();
php_info_print_table_header(2, "First column", "Second column");
php_info_print_table_row(2, "Entry in first row", "Another entry");
php_info_print_table_row(2, "Just to fill", "another row here");
php_info_print_table_end();
]]>
</programlisting>
<mediaobject>
<alt>Output of phpinfo()</alt>
<imageobject>
<imagedata fileref="en/internals2/ze1/zendapi/figures/zend.08-phpinfo-output.png"/>
</imageobject>
</mediaobject>
</example>
</sect3>
<sect3 xml:id="internals2.ze1.zendapi.printing.execution">
<title>Execution Information</title>
<para>
You can also print execution information, such as the current file
being executed. The name of the function currently being executed
can be retrieved using the function
<function>get_active_function_name</function>. This function
returns a pointer to the function name and doesn't accept any
arguments. To retrieve the name of the file currently being
executed, use <function>zend_get_executed_filename</function>.
This function accesses the executor globals, which are passed to
it using the <literal>TSRMLS_C</literal> macro. The executor globals
are automatically available to every function that's called
directly by Zend (they're part of the
<literal>INTERNAL_FUNCTION_PARAMETERS</literal> described earlier
in this chapter). If you want to access the executor globals in
another function that doesn't have them available automatically,
call the macro <literal>TSRMLS_FETCH()</literal> once in that
function; this will introduce them to your local scope.
</para>
<para>
Finally, the line number currently being executed can be retrieved
using the function <function>zend_get_executed_lineno</function>.
This function also requires the executor globals as arguments. For
examples of these functions, see <xref linkend="internals2.ze1.zendapi.example.exec-info"/>.
</para>
<example xml:id="internals2.ze1.zendapi.example.exec-info">
<title>Printing execution information.</title>
<programlisting role="c">
<![CDATA[
zend_printf("The name of the current function is %s<br>", get_active_function_name(TSRMLS_C));
zend_printf("The file currently executed is %s<br>", zend_get_executed_filename(TSRMLS_C));
zend_printf("The current line being executed is %i<br>", zend_get_executed_lineno(TSRMLS_C));
]]>
</programlisting>
<mediaobject>
<alt>Printing execution information</alt>
<imageobject>
<imagedata fileref="en/internals2/ze1/zendapi/figures/zend.09-execution-info.png"/>
</imageobject>
</mediaobject>
</example>
</sect3>
</sect2>
<!-- 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
-->
|