Beginners Learn PHP extensions automatically generated extension framework detailed (ii)

Source: Internet
Author: User
Tags learn php mcrypt php source code prototype definition zend

Objective

Previous article: Rookie learn PHP extension Hello World (a), do not ask why, forcibly with PHP extension say hello. For Ext_skel auto-generated frames, this article will be described in detail, as a memo.

Usage of body Ext_skel
./ext_skel--extname=Module[--proto=file] [--stubs=file]           [--xml[=file]] [--skel=dir] [--full-xml] [--no-help]--extname=Module   Moduleis the name ofYour extension (module name, which creates a subdirectory of the name in the current directory)--proto=file file contains prototypes ofFunctions toCreate (function prototype definition file)--stubs=file generate onlyfunctionStubsinchFile--xml Generate XML documentation tobe added toPhpdoc-cvs--skel=dir Path toThe Skeleton directory (set the skeleton generated directory, do not set the item by default under Ext/extname)--full-xml generate XML documentation forA self-contained extension (not yet implemented)--no-help don ' tTry  toIs nice andCreate commentsinchThe Code andHelper functions toTestifTheModuleCompiled (no various help comments are displayed in the generated code)
PHP Extensions-related processes

The start-up and termination of the 1.PHP program are conceptually divided into two different.

One is that when the 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, etc., and the data is resident memory, corresponding to a termination (php_mshutdown_function)

The other is when the PHP request starts, the start function before the request is not called (php_rinit_function), corresponding to the end of a request (php_rshutdown_function)

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

3. When a request arrives, PHP will quickly open up a new environment and rescan its own extensions, traversing their respective rinit methods (full name request initialization) (php_rinit_function), At this point an extension may initialize variables to be used in this request, and it will initialize variables in the later client (i.e. PHP script), and so on.

4. When the request passes through the business code and executes to the last, PHP starts the Recycle program and executes all the loaded extensions Rshutdown (full request Shutdown) (Php_rshutdown_function) method, Using the variables table in the kernel to do something, once the execution is done, it frees up everything that the request used, including all variables in the variable table, all the memory requested in this request, etc.

5. After the request processing is finished, the shutdown is also closed, PHP will enter the Mshutdown (full module Shutdown) (php_mshutdown_function) phase, at this time PHP to all extensions issued an ultimatum, if which extension has unfinished wish, Just put in your own Mshutdown method, this is the last chance, once PHP has extended the Mshutdown execution, it will enter the self-destruct program. (Clear the Last chance of unauthorized memory, otherwise the memory leaks out)

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 multi-threaded and multi-process diagrams

Config.m4

DNL represents comments off this line, as in PHP//. Why is DNL do not study, know is the note is good.

Dnl$Id$DNL CONFIG.M4 forExtension HELLOWORLDDNL CommentsinchThis file start with the string' DNL '. DNL Remove where necessary. This file would not be WORKDNL without editing.dnl If your extension references something external, use with:# #指定PHP模块的工作方式, dynamic compilation option, if you want to access the extension through. So, remove the previous DNL commentPhp_arg_with (HelloWorld, forHelloWorld Support,make sure that the comment are aligned:[--with-helloworld Include HelloWorld support]) DNL Otherwise use enable:# #指定PHP模块的工作方式, static compilation option, if you want to enable by enabling, remove DNL annotationsPhp_arg_enable (HelloWorld, whether to ENABLE HelloWorld support,make sure so the comment is aligned:[--enable-hellowor LD Enable HelloWorld support])ifTest"$PHP _helloworld"!="No"; ThenDNL Write More examples of tests ... dnl#--with-helloworld, check With-pathDNL search_path="/USR/LOCAL/USR"     # You might want to the change thisDNL search_for="/include/helloworld.h"  # likely want to change thisDnlifTest-r$PHP _helloworld/$SEARCH _for; Then # Path given as parameterDNL helloworld_dir=$PHP _helloworldDnlElse # Search Default Path listDNL ac_msg_checking ([ forHelloWorld filesinchDefault path]) DNL forIinch $SEARCH _path; DoDnlifTest-r$i/$SEARCH _for; ThenDNL helloworld_dir=$iDNL Ac_msg_result (foundinch $i) DNLfiDnl DoneDnlfiDNL DNLifTest-z"$HELLOWORLD _dir"; ThenDNL Ac_msg_result ([not found]) dnl ac_msg_error ([Please reinstall the HelloWorld distribution]) DNLfiDnl#--with-helloworld, add include pathDNL Php_add_include ($HELLOWORLD _dir/include) DNL#--with-helloworld Check for Lib and symbol presenceDNL Libname=helloworld# want to change thisDNL Libsymbol=helloworld# likely want to change thisDNL 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],[dnl-l$HELLOWORLD _dir/$PHP _libdir-LM DNL]) dnl# #用于说明这个扩展编译成动态链接库的形式DNL Php_subst (Helloworld_shared_libadd)# #用于指定有哪些源文件应该被编译, separated by a space between the file and the filePhp_new_extension (HelloWorld, HELLOWORLD.C,$ext _shared)fi
Php_helloworld.h

Found on the Internet a lot of textbooks written long ago, have in the header file reasons to declare the function. It seems that newer versions are not needed. Because the default generated framework does not see a word like "php_function (confirm_helloworld_compiled)" In the header file. So this file doesn't have to be too much of a control. (But the header file declares that the function to be implemented is a good habit)

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

#define PHP_HELLOWORLD_VERSION "0.1.0"
HELLOWORLD.C Code Structure

Many of the PHP_XXX macros are defined in the 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 "ext/standard/info.h"#include "php_helloworld.h"Static intLe_helloworld;# #PHP核心定义的一个宏, same as zend_function, for defining extension functions (this function is generated by default for validation purposes)Php_function (confirm_helloworld_compiled) {Char*arg = NULL;intArg_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 (); */    returnSUCCESS;}# #当module被卸载时运行Php_mshutdown_function (HelloWorld) {/* Uncomment if you have INI entries unregister_ini_entries (); */    returnSUCCESS;}# #当一个REQUEST请求初始化时运行Php_rinit_function (HelloWorld) {returnSUCCESS;}# #当一个REQUEST请求结束时运行Php_rshutdown_function (HelloWorld) {returnSUCCESS;}# #声明模块信息函数, the information that can be seen in PhpinfoPhp_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 had entries in php.ini display_ini_entries (); */}# #声明 (Introduction) Zend (PHP) function blockConstZend_function_entry helloworld_functions[] = {Php_fe (confirm_helloworld_compiled, NULL)/ * For testing, remove later. * /# #上一讲中就是在这里添加了自己定义的函数模块 Php_fe (HelloWorld, NULL)/*  */# #zend引擎认为结束的标记, the old version is "{Null,null,null}", after the PHP source code directly defined a macro php_fe_end, here directly with this. I mean, it's a lot more fun. # #如果遇到PHP_FE_END未定义undefine的问题, see appendix1   Php_fe_end /* Must be the last line in helloworld_functions[] * /};# #声明 Zend module, is not the feeling of the following module name is very familiar, right, is the previous article on the PHP process will be used, and now understand why the first to tell the process of it ~Zend_module_entry helloworld_module_entry = {Standard_module_header,"HelloWorld", Helloworld_functions, Php_minit (HelloWorld), Php_mshutdown (HelloWorld), Php_rinit (HelloWorld),/ * Replace with NULL if there 's nothing to do at request start */Php_rshutdown (HelloWorld),/ * Replace with NULL if there 's nothing to does at request end */Php_minfo (HelloWorld), php_helloworld_version, standard_module_properties};# #实现get_module () function#ifdef Compile_dl_helloworldZend_get_module (HelloWorld)#endif
Module structure

1. Include the header file (introduce the required macro, API definition, 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 export function (declaration for 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 (which is another macro) as the parameter, and the function name is prefixed with 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 the Zend function block;

Now that you have declared the export function, Zend does not know how to invoke it, so it must be introduced into the 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 a function appears in PHP and the name defined in 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 Zend.

5. Implement the Get_module () function;

This function is only used for dynamic loadable modules

6. Implement the export function.

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

PS: Part of the module is to learn this article, originally written, and later found that he wrote better than I have borrowed from the PHP extension code structure of the detailed

Appendix

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

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

‘s|PHP_FE_END|{NULL,NULL,NULL}|‘ ./ext/**/*.c# sed -i ‘s|ZEND_MOD_END|{NULL,NULL,NULL}|‘ ./ext/**/*.c

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

Beginners Learn PHP extensions automatically generated extension framework detailed (ii)

Related Article

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.