In the development process, for the sake of readability and maintainability of the code, it is necessary to have more than one code file, not just the skeleton file generated by Ext_skel. This article mainly introduces the next, multiple code files when we need to pay attention to what, and how to do.
My code file is as follows (slash for my extension)
// ext_skel 生成默认得模块相关的主要文件// 头文件// 源码文件// 编译的时候需要修改的文件
In this default generated skeleton file, we can complete a new extension as long as we perform the following steps.
删除config.m4的第10-12行开头的注释dnlphpize./configuremake && make install
With the above steps, we have completed an extension that can be tested by means of the DL ("slash.so"). If you write a class here, there is an introduction to the content of the link.
Here we introduce the addition of new classes and source files, and how to compile the configuration.
Add a new PHP class (Slash_http_util) and add a Getiv method to the class, and the PHP Code test code is as follows:
$httpUtilnew Slash_Http_Util();$httpUtil// 会输出 HELLO
Source file Slash_http_util.h
#ifndef SLASH_HTTP_UTIL_H#define SLASH_HTTP_UTIL_Hextern zend_class_entry *slash_http_util_ce;PHP_METHOD(slash_http_util, getIv);SLASH_MINIT_FUNCTION(http_util);#endif
Source file Slash_http_util.c
#ifdef Have_config_h#include "config.h"#endif#include "php.h"#include "php_ini.h"/* for Zend_alter_ini_entry * /#include "php_slash.h"#include "slash_http_util.h"Zend_class_entry *slash_http_util_ce; Php_method (Slash_http_util, Getiv) {php_printf ("HELLO"); Return_true;} Zend_function_entry slash_http_util_methods[] = {Zend_me (slash_http_util, Getiv, NULL, zend_acc_public| zend_acc_static) Php_fe_end}; Slash_minit_function (http_util) {zend_class_entry CE; Slash_init_class_entry (CE, slash_cn_http_util, slash_http_util_methods); Slash_http_util_ce = Zend_register_internal_class (&ce tsrmls_cc);returnSUCCESS;}
The above code completes the addition of the new class, and adds a method called Getiv to the class. The following need to modify my CONFIG.M4 configuration file, add slash_http_util.c this file. Open CONFIG.M4 to the last penultimate line, the default Ext_skel generated code is
PHP_NEW_EXTENSION(slash,slash.c,$ext_shared)
Modify, add slash_http_util.c to this file, note that there are spaces between the two *.c files, no punctuation
PHP_NEW_EXTENSION(slash,slash.c slash_http_util.c, $ext_shared)
After the completion of CONFIG.M4 modification, you also need to modify slash.c this file, by default in this file will have the following code
PHP_MINIT_FUNCTION(slash){ /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); */ return SUCCESS;}
We need to load our newly created class into the
PHP_MINIT_FUNCTION(slash){ /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); */ SLASH_STARTUP(http_util); return SUCCESS;}
With these steps, the new class is completely created and you can compile the module with Phpize, configure, and make. Finally, we test through the goals we set out.
Several issues to be aware of:
1. Modify CONFIG.M4 file, add source file
2. After modifying the config.m4 file, you need to re-execute phpize
3. Php_minit_function joins this new class in the module load function.
The above code if you completely copy may not be able to compile, because I custom renamed a few macros (PHP_SLASH.H), the actual development of the macro you can zend.
#define SLASH_CN_HTTP_UTIL "Slash_http_util"//{{{{{} PHP life cycle redefine#define Slash_minit_function (module) php_minit_function (slash_# #module)#define Slash_mshutdown_function (module) php_mshutdown_function (slash_# #module)#define Slash_rinit_function (module) php_rinit_function (slash_# #module)#define Slash_rshutdown_function (module) php_rshutdown_function (slash_# #module)#define SLASH_STARTUP (module) php_module_startup_n (slash_# #module) (init_func_args_passthru)// }}}//{{{} class init Mac#define Slash_init_class_entry (CE, Name, methods) Init_class_entry (CE, Name, methods)// }}}
The above code is for reference only, welcome to communicate.
PHP Extension Development Notes (2) configuration and compilation of multiple source files