[Translation][php extension development and embedded] Chapter 13-php INI settings
黄舟
Release: 2023-03-05 16:30:02
Original
1066 people have browsed it
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. .
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()
Copy after login
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:
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:
As you may have guessed, the first two parameters of this macro Indicates the name of the INI instruction and its default value. The third parameter is used to determine whether the engine allows this INI instruction to be modified (this will involve the access level issue introduced later in this chapter). The last parameter is a callback function, which Will be called every time the value of the INI directive changes. You will see the details of this parameter in the Modification Events section.
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.
Be sure Note that the value of char * is owned by the engine and must not be modified. Because of this, define your local variable used to temporarily store INI setting values as const. Of course, not all INI values are strings; There are other macros for getting integer, floating point and boolean values:
Usually what you want to know is the current value of the INI setting; however, as a supplement, there are several macros that can be used to read the unmodified INI setting value:
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 and External 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 Apache and 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.
int php_sample4_modify_greeting(zend_ini_entry *entry,
char *new_value, uint new_value_length,
void *mh_arg1, void *mh_arg2, void *mh_arg3,
int stage TSRMLS_DC);
Copy after login
各个参数的含义见下表:
参数名
含义
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 .
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn