A brief analysis of PHP extension Development Basics (2) _php tutorial

Source: Internet
Author: User
Tags php source code zts
If you are too lazy to figure out the full contents of the PHP extension package directory structure, there are three files that you must note:

CONFIG.M4: This is the build system configuration file in the UNIX environment, which will be followed by the configuration and installation.

Php_say_hello.h: This file is the header file for the extension module. Follow the C language consistent style, this can be placed in a number of custom structure, global variables and so on.

SAY_HELLO.C: This is the main program file of the extension module, and the final extension modules are all function portals here. Of course, you can plug all the program code into this, or you can follow the modular idea, and put each function module into different files.

The following sections focus on these three files.

Unix Build System Configuration

The first step in developing a PHP extension is not to write implementation code, but to configure the build System option first. Since we are developing under Linux, the configuration here is mainly related to CONFIG.M4.

About the build system configuration this piece, if write can write a lot of things, and the Unix system, even if I am interested in writing estimates that everyone is not interested to see, so here we withheld, just pick the key place to say, about CONFIG.M4 more details can refer to here.

Open the generated CONFIG.M4 file with the following content:

DNL $Id ___fckpd___4nbsp; DNL config.m4 for extension Say_hello dnl Comments in this file start with the string dnl. DNL Remove where necessary. This file won't 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 the comment is aligned:dnl [--with-say_hello Include Say_hello support]) DNL Otherwise use ENABLE:DNL php_ar G_enable (Say_hello, whether to ENABLE Say_hello support, DNL Make sure that the comment is ALIGNED:DNL [--enable-say_hel Lo Enable Say_hello support]) if test "$PHP _say_hello"! = "no"; Then DNL Write more examples of tests-here ... dnl #--with-say_hello-check With-path dnl search_path= "/usr/local/us R "# might want to the change this dnl search_for= '/include/say_hello.h ' # You are likely want to the 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 on default path]) dnl for i in $SEARCH _path; Do DNL if test-r $i/$SEARCH _for; Then DNL say_hello_dir= $i dnl ac_msg_result (found in $i) dnl fi dnl-Done dnl fi dnl dnl if test-z "$SAY _hello_dir"; Then DNL ac_msg_result ([no found]) dnl ac_msg_error ([Please reinstall the Say_hello distribution]) dnl fi dnl #--with-sa Y_hello, add include path DNL php_add_include ($SAY _hello_dir/include) DNL #--with-say_hello, check for Lib and Symbol presence DNL Libname=say_hello # Want to change this DNL Libsymbol=say_hello # your most likely want to Chan GE this dnl php_check_library ($LIBNAME, $LIBSYMBOL, dnl [dnl php_add_library_with_path ($LIBNAME, $SAY _hello_dir/lib, Say_hello_shared_libadd) DNL Ac_define (have_say_hellolib,1,[]) dnl],[dnl ac_msg_error ([wrong Say_hello lib version or L IB not found]) DNL],[dnl-l$say_hello_dir/lib-lm dnl]) dnl dnl php_subst (say_hello_shared_libadd) php_new_extension (SA Y_hello, SAY_HELLO.C, $ext _shared) fi don'tSee so much, because all the "DNL" begins with all the comments, so it really works in a few lines. Here are just a few lines to configure:

DNL If your extension references something external, use WITH:DNL php_arg_with (Say_hello, for Say_hello support, DNL make Sure the comment is aligned:dnl [--with-say_hello Include Say_hello support]) DNL Otherwise use ENABLE:DNL php_ar G_enable (Say_hello, whether to ENABLE Say_hello support, DNL Make sure that the comment is ALIGNED:DNL [--enable-say_hel Lo Enable Say_hello Support] I think everyone can read it, meaning "if your extension references an external component, use ..., otherwise use ...". Our Say_hello extension does not refer to external components, so remove "DNL" from the three lines below "Otherwise use enable" instead:

DNL Otherwise Use Enable:php_arg_enable (Say_hello, whether to enable Say_hello support, make sure that the comment is Ali gned: [--enable-say_hello enable Say_hello support]) save so that the build system configuration is done.

PHP extension and Zend_module structure analysis

The above can be seen as a preparation for the development of PHP extensions, the following is the core code to write. As mentioned above, writing PHP extensions is based on the Zend API and some macros, so if you want to write the core code, we first need to figure out the structure of PHP extension. Because a php extension is actually a zend_module_entry struct at the C level, this can be verified from "php_say_hello.h". Open "Php_say_hello.h", you will see how the line:

extern zend_module_entry Say_hello_module_entry; Say_hello_module_entry is the Say_hello extension of the C language corresponding elements, and the definition of its type zend_module_entry can be found in the PHP source code "zend/zend_modules.h" file, The following code is the definition of zend_module_entry:

typedef struct _ZEND_MODULE_ENTRY zend_module_entry; struct _zend_module_entry {unsigned short size; unsigned int Zend _api; unsigned char zend_debug; unsigned char zts; const struct _zend_ini_entry *ini_entry; const struct _ZEND_MODULE_DEP *deps; const char *name; const struct _zend_function_entry *functions; Int (*module_startup_func) (Init_func_args); Int (*module_shutdown_func) (Shutdown_func_args); Int (*request_startup_func) (Init_func_args); Int (*request_shutdown_func) (Shutdown_func_args); void (*info_func) (Zend_module_info_func_args); const char *version; size_t Globals_size; #ifdef ZTS ts_rsrc_id* globals_id_ptr; #else &n

http://www.bkjia.com/PHPjc/478780.html www.bkjia.com true http://www.bkjia.com/PHPjc/478780.html techarticle If you are too lazy to figure out the full contents of the PHP extension package directory structure, then there are three files inside you must note: config.m4: This is the build system configuration file in Unix environment, later ...

  • 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.