Beginners Learn PHP expansion of the automatic generation of expanded framework for detailed (II)

Source: Internet
Author: User
Tags documentation function prototype learn php mcrypt php script prototype definition zend

Reprint please attach this article address: http://blog.csdn.net/u011957758/article/details/72456298 preface

Previous: Rookie learn the PHP expansion of Hello World (a), do not ask why, forced with PHP expansion say hello. For the Ext_skel automatically generated framework, will be in this article to be detailed, as a memo. Body the use of Ext_skel

./ext_skel--extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module module is the name   of your extension (module name, which creates a subdirectory of the name in the current directory)
  --proto=file       file Contains prototypes of functions to create (function prototype definition file)
  --stubs=file       generate only function stubs in file
  --xml              generate XML documentation to is added to Phpdoc-cvs
  --skel=dir         path to the skeleton directory (set skeleton-generated directory, not set Place the item by default under Ext/extname)
  --full-xml         generate XML documentation for a self-contained extension
                     (not yet  Implemented)
  --no-help          don ' t try to be nice and create comments in the code and
                     helper functions to test if The module compiled (the generated code does not display various help annotations)
PHP and extension-related processes

The start and end of the 1.PHP program are conceptually two-divided.

When a PHP module is loaded, the module startup function is called by the engine (php_minit_function). This allows the engine to do some initialization such as resource type, register INI variable, and so on, and the data is resident memory, corresponding to a termination (php_mshutdown_function)

Another is the start of the PHP request, the request before the start function is not called (php_rinit_function), and corresponding to a request after the end of the termination (php_rshutdown_function)

2. With the launch of PHP, the Minit method of all the loaded extensions is started (full name module initialization, which is defined by each module itself). (php_minit_function), all executed once, in this time, the extension can define some of its own constants, classes, resources, and so on all the user-side PHP script to use the things. The things defined here will reside in memory and can be used by all requests until the PHP module is turned off.

3. When a request arrives, PHP quickly opens up a new environment and scans its own extensions, traversing their respective rinit methods (full name request initialization) (php_rinit_function), At this point an extension may initialize the variables that are used in this request, and initialize the variables in the later client (that is, the PHP script), and so on.

4. When the request passes through the business code and executes to the end, PHP launches the Recycle program and executes all the extended Rshutdown (the full request Shutdown) (Php_rshutdown_function) method that is loaded. Use the variables in the kernel to do things like this, once the execution is over, it will release all the things that the request used, including all variables in the variable table, all the memory requested in this request, and so on.

5. After the request processing, the shutdown is also closed, PHP into the Mshutdown (full name Module Shutdown) (php_mshutdown_function) phase, at this time, PHP will issue an ultimatum to all extensions, if which expansion and unfinished wish, Put in your own Mshutdown method, this is the last chance, once PHP has extended mshutdown execution, will go into the self-destruct program. (Clears the last chance of unauthorized memory, otherwise memory leaks)

Summary, I understand the process:
Php_minit_function (one process executes once)
|
Execute a lot of php_rinit_function
|
Execute a lot of php_rshutdown_function
|
Php_mshutdown_function (one process executes once)

Attach multithreaded and multi-process diagrams

CONFIG.M4

DNL on behalf of the note dropped this line, as in PHP//. Why is DNL not study, know is a remark on the good.

DNL $Id $ dnl config.m4 for extension HelloWorld dnl Comments in this file start with the string ' DNL '. DNL Remove where necessary.

This file is not work DNL without editing. DNL If your extension references something external, use with: # #指定PHP模块的工作方式, dynamic compilation options, if you want to access the extension through. So, remove the previous DNL comment Php_arg _with (HelloWorld, for HelloWorld support, make sure this comment is aligned: [--with-helloworld include HelloWorld support]) DNL Otherwise use enable: # #指定PHP模块的工作方式, static compilation options, if you want to enable by using the Enable, remove DNL comment php_arg_enable (           HelloWorld, whether to enable HelloWorld support, make sure this comment is aligned: [--enable-helloworld Enable HelloWorld support]) if test "$PHP _helloworld"!= "no"; Then DNL Write more examples of tests ... dnl #--with-helloworld-> check with-path dnl search_path= '/usr/l OCAL/USR "# You might the want to the change this dnl search_for= '/include/helloworld.h ' # you most likely want to change This dnl if Test-r $PHP _helloworld/$SEARCH _for; Then # path given as parameter dnl helloworld_dir= $PHP _helloworld dnl Else # Search default Path list DNL ac_msg _checking ([for HelloWorld files in Default path]) dnl for i in $SEARCH _path; Do DNL if test-r $i/$SEARCH _for; Then DNL helloworld_dir= $i dnl ac_msg_result (found in $i) dnl fi dnl do dnl fi dnl dnl If Test-z "$HELLOWORLD _dir"; Then DNL ac_msg_result ([not found]) dnl ac_msg_error ([Please reinstall the HelloWorld distribution]) Dnl fi D NL #--with-helloworld-> add include path dnl php_add_include ($HELLOWORLD _dir/include) DNL #--with-helloworld-&G T Check for Lib and symbol presence DNL Libname=helloworld # Your may want to change this DNL Libsymbol=helloworld # Most likely want to change this dnl php_check_library ($LIBNAME, $LIBSYMBOL, dnl [DNL Php_add_library_with_path ($ Libname, $HELLOWORLD _dir/$PHP _libdir, Helloworld_shared_libadd) dnl Ac_define (Have_helloworldliB,1,[]) dnl],[dnl ac_msg_error ([wrong HelloWorld Lib version or Lib not found]) DNL],[ ir/$PHP _libdir-lm DNL]) DNL # #用于说明这个扩展编译成动态链接库的形式 DNL php_subst (helloworld_shared_libadd) # #用于指定有哪些源文件应该被编译,
 Files and files are separated by spaces php_new_extension (HelloWorld, HELLOWORLD.C, $ext _shared) fi
php_helloworld.h

Found that many of the online textbooks have been written long ago, there are in the first document reason to affirm the function. It seems that newer versions are not needed. Because the default generated framework does not see the words "php_function (confirm_helloworld_compiled)" In the header file. So this file doesn't have to be too much for him. (But the header file declares that the function to be implemented is a good habit)

You know, HELLOWORLD.C, the version number that's going to be used below is defined here.

#define Php_helloworld_version "0.1.0"
HELLOWORLD.C Code Structure

Many of the macros in PHP_XXX are defined in Main/php.h.

#ifdef have_config_h #include "config.h" #endif # #包含头文件 (Introduction of required macros, API definitions, etc.) #include "php.h" #include "php_ini.h" #include "E

Xt/standard/info.h "#include" php_helloworld.h "static int le_helloworld; # #PHP核心定义的一个宏, same as zend_function, used to define extension functions (This function is the default generated by the system, used for validation) php_function (confirm_helloworld_compiled) {char *arg
    = NULL;
    int Arg_len, Len;

    Char *STRG;
    if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "s", &arg, &arg_len) = = failure) {return; Len = spprintf (&STRG, 0, "congratulations! You have successfully modified EXT/%.78S/CONFIG.M4.
    Module%.78s is now compiled into PHP. "," HelloWorld ", Arg);
Return_stringl (STRG, Len, 0);
    # #定义PHP中可以调用的函数 Php_function (HelloWorld) {php_printf ("Hello world!\n");
Return_true; # #初始化module时运行 Php_minit_function (HelloWorld) {/* If you have INI entries, uncomment these lines Register_ini
    _entries ();
* * return SUCCESS;
 # #当module被卸载时运行 Php_mshutdown_function (HelloWorld) {   /* Uncomment this line if you have INI entries unregister_ini_entries ();
* * return SUCCESS;

# #当一个REQUEST请求初始化时运行 Php_rinit_function (HelloWorld) {return SUCCESS;}

# #当一个REQUEST请求结束时运行 Php_rshutdown_function (HelloWorld) {return SUCCESS}
    # #声明模块信息函数, that is, the information that can be seen in Phpinfo php_minfo_function (HelloWorld) {Php_info_print_table_start ();
    Php_info_print_table_header (2, "HelloWorld support", "enabled");

    Php_info_print_table_end ();
    /* Remove Comments If you are have entries in php.ini display_ini_entries (); */} # #声明 (introduced) Zend (PHP) function block const Zend_function_entry helloworld_functions[] = {Php_fe (confirm_helloworld_compiled, NU LL)/* For testing, remove later. * * * #上一讲中就是在这里添加了自己定义的函数模块 Php_fe (HelloWorld, NULL)/* * * * * #zend引擎认为结束的标记, the old version is "{Null,null,null}", the back ph P Source code directly defines a macro php_fe_end, this is the direct use of this.  It all means something, but it's been a lot of fun. # #如果遇到PHP_FE_END未定义undefine的问题, see Appendix 1 php_fe_end/* Must is the last line in helloworld_functions[]

*/
}; # #声明Zend module, is not the feeling of the following module name is very familiar with, is the previous article on the PHP process will be used, now understand why to speak the process first ~ Zend_module_entry Helloworld_module_entry = {Standard_ Module_header, "HelloWorld", Helloworld_functions, Php_minit (HelloWorld), Php_mshutdown (HelloWorld), P Hp_rinit (HelloWorld),/* Replace with NULL if there ' s no to doing at request start/Php_rshutdown (HelloWorld)
    ,/* Replace with NULL if there ' s no to do in Request end */Php_minfo (HelloWorld), Php_helloworld_version,

Standard_module_properties};
 # #实现get_module () function #ifdef Compile_dl_helloworld zend_get_module (HELLOWORLD) #endif
Module Structure

1. Include header file (introduction of required macros, API definitions, etc.);

The header file that the module must contain has only one php.h, which is located in the main directory. This file contains the various macro and API definitions necessary to build the module.

2. Declare the exported function (for the declaration of the Zend function block);

The Zend_function macro declares a new C function that is compiled using the ZEND internal API. This c function is a void type, with internal_function_parameters (this is another macro) as a parameter, and the function name is prefixed by zif_.
Php_function is the same as this one. There are already defined macros in the/main/php.h

#define Php_function            Zend_function

3. declare Zend function block;

Now that you have declared the exported function, Zend does not know how to invoke it, so it must be introduced into Zend. The introduction of these functions is done through an array containing n zend_function_entry structures. Each item of an array corresponds to an externally visible function, each containing the name that appears in PHP for a function and the name defined in the C code.

4. Declare the Zend module;

The information for the Zend module is stored in a structure called Zend_module_entry, which contains all the module information that needs to be provided to the Zend.

5. Implement Get_module () function;

This function is only for dynamic loadable modules

6. Implement the exported function.

Implement the function you want to extend, php_function (HelloWorld)

PS: module part is to learn this article, was written well, and later found that he wrote better than me on the reference to the PHP extended code structure of the appendix

1.error: ' Php_fe_end ' undeclared here (no in a function) error.

Reason: Because the Zend engine version is too old.
1, switch to the source directory of PHP,
2, execute the following two lines

# sed-i ' s| php_fe_end| {null,null,null}| ' ./ext/**/*.c
# sed-i ' s| zend_mod_end| {null,null,null}| ' ./ext/**/*.c

3. Switch to MCrypt directory, such as php-5.x.x/ext/mcrypt/. Perform the make command again, and everything is back to normal.

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.