Analysis of PHP Extension development BASICS (2)

Source: Internet
Author: User
Tags zts

If you are too lazy to figure out all the contents of the directory structure of the PHP extension package, you must note that there are three files in it:

Config. m4: This is the Build System configuration file in the Unix environment, which will be used to generate configuration and install later.

Php_say_hello.h: this file is the header file of the extension module. Following the consistent style of C language, you can place some custom struct and global variables.

Say_hello.c: This is the main program file of the extension module. The function entry of the final extension module is here. Of course, you can put all program code here, or you can follow the modular idea to put each function module into different files.

The following content focuses on these three files.

Unix Build System Configuration

The first step in developing PHP extension components is not to write implementation code, but to configure the Build System option first. Because we are developing in Linux, the configuration here is mainly related to config. m4.

The configuration of Build System can be written in many ways and is related to many things of Unix systems. Even if I am interested in writing it, we are not interested in reading it. So here we will proceed from the following, just pick the key points and talk about config. for more details about m4, refer to here.

Open the generated config. m4 file. The content is roughly as follows:

Dnl $ Id ___ FCKpd ___ 4 nbsp; dnl config. m4 for extension say_hello dnl Comments in this file start with the string dnl. dnl Remove where necessary. this file will not work dnl without editing. dnl If your extension references something external, use with: dnl PHP_ARG_WITH (say_hello, for say_hello support, dnl Make sure that the comment is aligned: dnl [-- with-say_hello Include say_hello support]) dnl Otherwise use enable: dnl PHP_ARG_ENABLE (say_hello, whether to enable say_hello support, dnl Make sure that the comment is aligned: dnl [-- enable-say_hello Enable say_hello support]) if test "$ success "! = "No"; then dnl Write more examples of tests here... dnl # -- with-say_hello-> check with-path dnl SEARCH_PATH = "/usr/local/usr" # you might want to change this dnl SEARCH_FOR = "/include/say_hello.h" # you most likely want to change this dnl if test-r $ PHP_SAY_HELLO/$ SEARCH_FOR; then # path given as parameter dnl SAY_HELLO_DIR = $ PHP_SAY_HELLO dnl else # search default path list dnl AC_MSG_CHECKING ([For say_hello files in default path]) dnl for I in $ SEARCH_PATH; do dnl if test-r $ I/$ SEARCH_FOR; then dnl outcome = $ I dnl AC_MSG_RESULT (found in $ I) dnl fi dnl done dnl fi dnl if test-z "$ SAY_HELLO_DIR"; then dnl AC_MSG_RESULT ([not found]) dnl AC_MSG_ERROR ([Please reinstall the say_hello distribution]) dnl fi dnl # -- with-say_hello-> add include path dnl PHP_ADD_INCLUDE ($ SAY_HELLO_D IR/include) dnl # -- with-say_hello-> check for lib and symbol presence dnl LIBNAME = say_hello # you may want to change this dnl LIBSYMBOL = say_hello # you most likely want to change this dnl PHP_CHECK_LIBRARY ($ LIBNAME name, $ LIBSYMBOL, dnl [dnl encode ($ LIBNAME, $ SAY_HELLO_DIR/lib, alias) dnl AC_DEFINE (delimiter, 1, []) dnl], [dnl AC_MSG_ERROR ([wrong say_hello Lib version or lib not found]) dnl], [dnl-L $ SAY_HELLO_DIR/lib-lm dnl]) dnl PHP_SUBST (bytes) PHP_NEW_EXTENSION (say_hello, say_hello.c, $ ext_shared) fi should not read so much, because all the comments starting with "dnl" really work a few lines. Only the following lines need to be configured:

Dnl If your extension references something external, use with: dnl PHP_ARG_WITH (say_hello, for say_hello support, dnl Make sure that the comment is aligned: dnl [-- with-say_hello Include say_hello support]) dnl Otherwise use enable: dnl PHP_ARG_ENABLE (say_hello, whether to enable say_hello support, dnl Make sure that the comment is aligned: dnl [-- enable-say_hello Enable say_hello support]) I want everyone You can see clearly, that is, "If your extension references external components, use ..., Otherwise, use ...". Our say_hello extension does not reference external components, so remove the "dnl" in the three lines under "Otherwise use enable" and change it:

Dnl Otherwise use enable: PHP_ARG_ENABLE (say_hello, whether to enable say_hello support, Make sure that the comment is aligned: [-- enable-say_hello Enable say_hello support]) save, in this case, the Build System configuration is complete.

PHP Extension and Zend_Module Structure Analysis

The above can be seen as preparations for developing PHP extensions. The core code should be written below. As mentioned above, PHP extensions are written based on Zend APIs and some macros. Therefore, if you want to write core code, you must first understand the structure of PHP Extension. Because a PHP Extension is actually a zend_module_entry struct in C language, this can be confirmed from "php_say_hello.h. Open "php_say_hello.h" and you will see a line in it:

Extern zend_module_entry say_hello_module_entry; say_hello_module_entry is the corresponding element of say_hello extension C language. The definition of its type zend_module_entry can be found in the PHP source code file "Zend/zend_modules.h, the following code defines zend_module_entry:

Typedef struct _ struct; struct _ detail {unsigned short size; unsigned int zend_api; unsigned char zend_debug; unsigned char zts; const struct _ detail * ini_entry; const struct _ detail * deps; const char * name; const struct _ zend_function_entry * functions; int (* functions) (INIT_FUNC_ARGS); int (* functions) (SHUTDOWN_FUNC_ARGS); int (* functions) (INIT_FUNC_ARGS ); int (* response) (SHUTDOWN_FUNC_ARGS); void (* info_func) (response); const char * version; size_t globals_size; # ifdef ZTS ts_rsrc_id * globals_id_ptr; # else & n

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.