In the development process, for the sake of readability and maintainability of code, it is necessary to have more than one code file, but not the skeleton file generated by Ext_skel.
This article mainly introduces the following. Multiple code files at a time. What we need to pay attention to and how to do it.
My code files such as the following (slash for my extension)
// ext_skel 生成默认得模块相关的主要文件// 头文件// 源代码文件// 编译的时候须要改动的文件
In this default generated skeleton file, we can complete a new extension as long as we run the following steps.
删除config.m4的第10-12行开头的凝视dnlphpize./configuremake && make install
With the above steps, we are finished with an extension that can be tested by means of a DL ("slash.so"). Suppose you write a class that is described in the previous article.
Here we describe how to add new classes and source code files, how to compile configurations, and so on.
Add a new PHP class (Slash_http_util), and add a Getiv method to the class, the PHP code test code such as the following:
$httpUtilnew Slash_Http_Util();$httpUtil// 会输出 HELLO
Source code Files 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 code Files 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 change my config.m4 profile, add slash_http_util.c this file.
Open the CONFIG.M4 to the last penultimate line. The code generated by default Ext_skel is
PHP_NEW_EXTENSION(slash,slash.c,$ext_shared)
Change, add slash_http_util.c This file, note that there is a space between two *.c files, no punctuation
PHP_NEW_EXTENSION(slash,slash.c slash_http_util.c, $ext_shared)
After the change of CONFIG.M4, still need to change 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 it by the goal we set out to begin with.
Several issues to note:
1. Change the config.m4 file to add source code files
2. After changing the config.m4 file. Need to run Phpize && again./configure && make
3. In the module (slash.c file) load function php_minit_function Add this new class.
The above code assumes that you completely copy may not be able to compile, because I have renamed several macros (PHP_SLASH.H). A macro that you can zend in real-world development.
#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 exchange.
PHP Extension Development Notes (2) configuration and compilation of multiple source code files