internals2.ze1.zendapi.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 1,583 行 · 第 1/5 页
HTML
1,583 行
debug extensions. </p> <p class="para"> The following table describes the contents of the major directories. </p> <table class="informaltable"> <colgroup> <col align="left" width="1" /> <col align="left" width="3" /> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Directory</td> <td colspan="1" rowspan="1" align="left">Contents</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><var class="filename">php-src</var></td> <td colspan="1" rowspan="1" align="left"> Main PHP source files and main header files; here you'll find all of PHP's API definitions, macros, etc. (important). Everything else is below this directory. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><var class="filename">php-src/ext</var></td> <td colspan="1" rowspan="1" align="left"> Repository for dynamic and built-in modules; by default, these are the "official" PHP modules that have been integrated into the main source tree. From PHP 4.0, it's possible to compile these standard extensions as dynamic loadable modules (at least, those that support it). </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><var class="filename">php-src/main</var></td> <td colspan="1" rowspan="1" align="left"> This directory contains the main php macros and definitions. (important) </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><var class="filename">php-src/pear</var></td> <td colspan="1" rowspan="1" align="left"> Directory for the PHP Extension and Application Repository. This directory contains core PEAR files. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><var class="filename">php-src/sapi</var></td> <td colspan="1" rowspan="1" align="left"> Contains the code for the different server abstraction layers. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><var class="filename">TSRM</var></td> <td colspan="1" rowspan="1" align="left"> Location of the "Thread Safe Resource Manager" (TSRM) for Zend and PHP. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><var class="filename">ZendEngine2</var></td> <td colspan="1" rowspan="1" align="left"> Location of the Zend Engine files; here you'll find all of Zend's API definitions, macros, etc. (important). </td> </tr> </tbody> </colgroup> </table> <p class="para"> Discussing all the files included in the PHP package is beyond the scope of this chapter. However, you should take a close look at the following files:<ul class="itemizedlist"> <li class="listitem"> <p class="para"> <var class="filename">php-src/main/php.h</var>, located in the main PHP directory. This file contains most of PHP's macro and API definitions. </p> </li> <li class="listitem"> <p class="para"> <var class="filename">php-src/Zend/zend.h</var>, located in the main Zend directory. This file contains most of Zend's macros and definitions. </p> </li> <li class="listitem"> <p class="para"> <var class="filename">php-src/Zend/zend_API.h</var>, also located in the Zend directory, which defines Zend's API. </p> </li> </ul> You should also follow some sub-inclusions from these files; for example, the ones relating to the Zend executor, the PHP initialization file support, and such. After reading these files, take the time to navigate around the package a little to see the interdependencies of all files and modules - how they relate to each other and especially how they make use of each other. This also helps you to adapt to the coding style in which PHP is authored. To extend PHP, you should quickly adapt to this style. </p> <div id="internals2.ze1.zendapi.layout.conventions" class="sect3"> <h4 class="title">Extension Conventions</h4> <p class="para"> Zend is built using certain conventions; to avoid breaking its standards, you should follow the rules described in the following sections. </p> </div> <div id="internals2.ze1.zendapi.layout.macros" class="sect3"> <h4 class="title">Macros</h4> <p class="para"> For almost every important task, Zend ships predefined macros that are extremely handy. The tables and figures in the following sections describe most of the basic functions, structures, and macros. The macro definitions can be found mainly in <var class="filename">zend.h</var> and <var class="filename">zend_API.h</var>. We suggest that you take a close look at these files after having studied this chapter. (Although you can go ahead and read them now, not everything will make sense to you yet.) </p> </div> <div id="internals2.ze1.zendapi.layout.memory-management" class="sect3"> <h4 class="title">Memory Management</h4> <p class="para"> Resource management is a crucial issue, especially in server software. One of the most valuable resources is memory, and memory management should be handled with extreme care. Memory management has been partially abstracted in Zend, and you should stick to this abstraction for obvious reasons: Due to the abstraction, Zend gets full control over all memory allocations. Zend is able to determine whether a block is in use, automatically freeing unused blocks and blocks with lost references, and thus prevent memory leaks. The functions to be used are described in the following table: <table class="informaltable"> <colgroup> <col align="left" width="1" /> <col align="left" width="1" /> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Function</td> <td colspan="1" rowspan="1" align="left">Description</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>emalloc()</b></td> <td colspan="1" rowspan="1" align="left">Serves as replacement for <b>malloc()</b>.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>efree()</b></td> <td colspan="1" rowspan="1" align="left">Serves as replacement for <b>free()</b>.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>estrdup()</b></td> <td colspan="1" rowspan="1" align="left">Serves as replacement for <b>strdup()</b>.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>estrndup()</b></td> <td colspan="1" rowspan="1" align="left">Serves as replacement for <b>strndup()</b>. Faster than <b>estrdup()</b> and binary-safe. This is the recommended function to use if you know the string length prior to duplicating it.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>ecalloc()</b></td> <td colspan="1" rowspan="1" align="left">Serves as replacement for <b>calloc()</b>.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>erealloc()</b></td> <td colspan="1" rowspan="1" align="left">Serves as replacement for <b>realloc()</b>.</td> </tr> </tbody> </colgroup> </table> <b>emalloc()</b>, <b>estrdup()</b>, <b>estrndup()</b>, <b>ecalloc()</b>, and <b>erealloc()</b> allocate internal memory; <b>efree()</b> frees these previously allocated blocks. Memory handled by the <b>e*()</b> functions is considered local to the current process and is discarded as soon as the script executed by this process is terminated. <div class="warning"><b class="warning">Warning</b> <p class="para"> To allocate resident memory that survives termination of the current script, you can use <b>malloc()</b> and <b>free()</b>. This should only be done with extreme care, however, and only in conjunction with demands of the Zend API; otherwise, you risk memory leaks. </p> </div> Zend also features a thread-safe resource manager to provide better native support for multithreaded Web servers. This requires you to allocate local structures for all of your global variables to allow concurrent threads to be run. Because the thread-safe mode of Zend was not finished back when this was written, it is not yet extensively covered here. </p> </div> <div id="internals2.ze1.zendapi.layout.dir-and-file" class="sect3"> <h4 class="title">Directory and File Functions</h4> <p class="para"> The following directory and file functions should be used in Zend modules. They behave exactly like their C counterparts, but provide virtual working directory support on the thread level. <table class="informaltable"> <colgroup> <col align="left" width="0" /> <col align="left" width="0" /> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Zend Function</td> <td colspan="1" rowspan="1" align="left">Regular C Function</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>V_GETCWD()</b></td> <td colspan="1" rowspan="1" align="left"><a href="function.getcwd.html" class="function">getcwd()</a></td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>V_FOPEN()</b></td> <td colspan="1" rowspan="1" align="left"><a href="function.fopen.html" class="function">fopen()</a></td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>V_OPEN()</b></td> <td colspan="1" rowspan="1" align="left"><b>open()</b></td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>V_CHDIR()</b></td> <td colspan="1" rowspan="1" align="left"><a href="function.chdir.html" class="function">chdir()</a></td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>V_GETWD()</b></td> <td colspan="1" rowspan="1" align="left"><b>getwd()</b></td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>V_CHDIR_FILE()</b></td> <td colspan="1" rowspan="1" align="left"> Takes a file path as an argument and changes the current working directory to that file's directory. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>V_STAT()</b></td> <td colspan="1" rowspan="1" align="left"><a href="function.stat.html" class="function">stat()</a></td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left"><b>V_LSTAT()</b></td> <td colspan="1" rowspan="1" align="left"><a href="function.lstat.html" class="function">lstat()</a></td> </tr> </tbody> </colgroup> </table></p> </div> <div id="internals2.ze1.zendapi.layout.string-handling" class="sect3"> <h4 class="title">String Handling</h4> <p class="para"> Strings are handled a bit differently by the Zend engine than other values such as integers, Booleans, etc., which don't require additional memory allocation for storing their values. If you want to return a string from a function, introduce a new string variable to the symbol table, or do something similar, you have to make sure that the memory the string will be occupying has previously been allocated, using the aforementioned <b>e*()</b> functions for allocation. (This might not make much sense to you yet; just keep it somewhere in your head for now - we'll get back to it shortly.) </p> </div> <div id="internals2.ze1.zendapi.layout.complex-types" class="sect3"> <h4 class="title">Complex Types</h4> <p class="para"> Complex types such as arrays and objects require different treatment. Zend features a single API for these types - they're stored using hash tables. </p> <blockquote><p><b class="note">Note</b>: To reduce complexity in the following source examples, we're only
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?