Home >Backend Development >PHP Tutorial >[Translation][php extension development and embedded] Chapter 13-php INI settings
INI setting
and the previous one Like the superglobal variables and persistent constants you saw in Chapter 1, php.ini values must be defined in the extended MINIT block. However, unlike other features, the definition of INI options consists only of simple start/stop lines. .
PHP_MINIT_FUNCTION(sample4) { REGISTER_INI_ENTRIES(); return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(sample4) { UNREGISTER_INI_ENTRIES(); return SUCCESS; }
Define and access INI settings
The INI instruction itself is above the MINIT function in the source code file, use The following macros are defined completely independently. One or more INI instructions can be defined between these two macros:
PHP_INI_BEIGN() PHP_INI_END()
These two macro functions are similar to ZEND_BEGIN_MODULE_GLOBALS()/ZEND_END_MODULE_GLOBALS(). However, this is not a typdef structure, but a framework organization for the definition of static data instances:
static zend_ini_entry ini_entries[] = { {0,0,NULL,0,NULL,NULL,NULL,NULL,NULL,0,NULL,0,0,NULL} };
As you can see, it defines a vector of zend_ini_entry values, ending with an empty record. This is the same as the static you saw earlier The definition of vector function_entry is consistent.
##Simple INI settings
Now, you already have an INI structure for It is used to define INI instructions and the mechanism for engine registration/uninstallation of INI settings, so we can actually define some INI instructions for your extension. Suppose your extension exposes a greeting function, just like Chapter 5 "Your The same as in the first extension", but you can customize it if you want to say hello:
PHP_FUNCTION(sample4_hello_world) { php_printf("Hello World!\n"); }
The simplest and most direct way is to define an INI command and give it a default value "Hello world!":
###include "php_ini.h"
PHP_INI_BEGIN()
PHP_INI_ENTRY("sample4.greeting", "Hello World",
PHP_INI_ALL, NULL)
PHP_INI_END()
Translation Note: If you and the translator When you encounter results that are inconsistent with the expected results of the original work, please add a "REGISTER_INI_ENTRIES();" call to your MINIT() function during testing, and make sure that the call is executed after allocating global space in your MINIT.
Now that your INI settings have been defined, you just need to use them in your greeting function.PHP_FUNCTION(sample4_hello_world)
{
const char *greeting = INI_STR("sample4.greeting");
php_printf("%s\n", greeting);
}
long lval = INI_INT("sample4.intval");
double dval = INI_FLT("sample4.fltval");
zend_bool bval = INI_BOOL("sample4.boolval");
const char *strval = INI_ORIG_STR("sample4.stringval");
long lval = INI_ORIG_INT("sample4.intval");
double dval = INI_ORIG_FLT("sample4.fltval");
zend_bool bval = INI_ORIG_BOOL("sample4.boolval");
In this example, the name of the INI instruction "sample4.greeting" adds the extension name as a prefix to ensure that it will not conflict with the INI instruction names exposed by other extensions. For private extensions , this prefix is not required, but is encouraged for public extensions for commercial or open source releases.
Access Level
For INI instructions, there is always a default value to start with. In most cases, the ideal is to leave the default value unchanged; however, for some special circumstances or specific actions within the script, these values may Needs to be modified. As shown in the following table, the value of the INI instruction may be modified at the following three points:
Access Level |
Meaning |
||||||||||
SYSTEM |
is located in php.ini#, or in the httpd.conf configuration file of apache 7b799fe73e35dcfdc019b13f54de80e5 and 017421ec4e1d87ae7f66fefe80084b1dExternal directive,affects the startup phase of the engine , can be considered as INI#"global"Value . |
||||||||||
##PERDIR ### |
is located in the httpd.conf configuration file of Apache7b799fe73e35dcfdc019b13f54de80e5 and 017421ec4e1d87ae7f66fefe80084b1d in the directive , or the directory or virtual directory where the request script is located The .htaccess file under the host and other apacheINI## set elsewhere before processing the request #Command. |
||||||||||
##USER
|
Once the script starts executing , can only be modified by calling the user space function ini_set() INIset. |
参数名 |
含义 |
entry |
Points to the actual storage of the engineINIInstruction item.This structure provides Current value , original value , belonging module , and some other codes below (zend_ini_entrystructure structure)Listed information |
new_value |
The value to be set.If the processor returnsSUCCESS,This value will be set to entry->value,At the same time if entry->orig_value is currently not set , will set the current value to entry->orig_value,and set entry->modifiedmarker.The length of this string is passed new_value_lengthPass . |
##mh_arg1, 2, 3 |
These 3 pointers correspond to the data pointers given when the INI instruction is defined (zend_ini_entryThe 3 members with the same name).In fact ,These values are used by the engine for internal processing,You don’t need to care about them. |
##stage |
##ZEND_INI_STAGE_ series One of 5 values: STARTUP, SHUTDOWN, ACTIVATE, DEACTIVATE, RUNTIME. These constants correspond to MINIT, MSHUTDOWN, RINIT, RSHUTDOWN, and active script execution . |