PHP initialization extension method: first modify the prototype declaration near the user space function in [php_hello.h]; then modify the [hello.c] file; then add a [# at the top of the file [hello.c] include]; finally modify [hello_world] to use the value of INI.
How to initialize the extension in PHP:
Initialize the configuration of the extension through php.ini
Zend engine provides two ways to manage ini values.
Suppose you want to define a value in php.ini for your extension,hello.greeting
, which holds the value that will be used in thehell_world()
function Greeting string. You need to add some code tohello.c
andphp_hello.h
, and make some key changes to thehell_module_entry
structure.
Related learning recommendations:PHP programming from entry to proficiency
1. The first step: modifyphp_hello.h
Close to the prototype declaration of the user space function
PHP_MINIT_FUNCTION(hello); PHP_MSHUTDOWN_FUNCTION(hello);// 初始化关键点 PHP_FUNCTION(hello_world);// 其它通用 PHP_FUNCTION(hello_long);
2. Step 2: Modify thehello.c
file
to remove the ## in the current version #hello_module_entryReplace it with the following list:
zend_module_entry hello_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STRANDARD_MODULE_HEADER, #endif PHP_HELLO_WORLD_EXTENAME, hello_functions, PHP_MINIT(hello), PHP_MSHUTDOWN(hello), NULL,NULL,NULL, #if ZEND_MODULE_API_NO >= 20010901 PHP_HELLO_WORLD_VERSION, #endif STANDARD_MODULE_PROERTIES } PHP_INI_BEGIN() PHP_INI_ENTRY("hello.greeting","hello world",PHP_INI_ALL,NULL) PHP_INI_END() PHP_MINIT_FUNCTION(hello) { REGISTER_INI_ENTRIES(); return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(hello) { UNREGISTER_INI_ENTRIES(); return SUCCESS; }
#includes at the top of the filehello.c
Add a#include
next to, so that the correct header file that supports INI can be activated:
#ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "php_hello.h"
hello_worldto use the value of
INI:
PHP_FUNCTION(hello_world) { RETURN_STRING(INI_STR("hello.greeting"),1); }
INI_STR()The value returned. This is because, before entering the PHP variable stack, it is a static string. In fact, if you try to modify the returned string, the PHP execution environment will become unstable or even crash.
INI_STR()to obtain the
hello.greetingentry The current string.
INI_INT, etc. Due to different setting parameters, it may cause functions like
ini_sethard to use.
The above is the detailed content of How to initialize extensions in PHP?. For more information, please follow other related articles on the PHP Chinese website!