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 generate the default module related main file php_slash.h//header file SLASH.C//source file CONFIG.M4//Compile the files that need to be modified
In this default generated skeleton file, we can complete a new extension as long as we perform the following steps.
Delete Config.m4 10–12 line at the beginning of the comment 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:
$httpUtil = new Slash_http_util (); $httpUtil->getiv (); Will output 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); return SUCCESS;}
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 SU ccess;}
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, met hods)//}}}
The above code is for reference only, welcome to communicate.