INI Settings for PHP

Source: Internet
Author: User
Tags definition ini

INI settings

Like the super global variables and persistent constants you see in the previous chapter, the php.ini value must be defined in the extended Minit code block. However, unlike other features, the INI option is defined only by a simple start/stop line.

Php_minit_function (sample4)  
{  
    register_ini_entries ();  
    return SUCCESS;  
}  
Php_mshutdown_function (sample4)  
{  
    unregister_ini_entries ();  
    return SUCCESS;  
}

Define and access the INI settings

The INI directive itself is in the source file Minit function above, use the following macro completely independent definition, between these two macros can define one or more INI directives:

Php_ini_beign ()  
php_ini_end ()

These two macro functions are similar to the Zend_begin_module_globals ()/zend_end_module_globals (). But this is not a typdef structure, but a framework organization defined for 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 consistent with the definition of the static vector function_entry you saw earlier.

Simple INI Settings

Now that you have an INI structure that defines the INI directive, and the engine registration/uninstall INI setting mechanism, we can actually define some INI directives for your extension. Let's say your extension exposes a greeting function, like the 5th chapter "Your first extension", but you want to make it a custom to say hello:

Php_function (Sample4_hello_world)  
{  
    php_printf ("Hello world!\n");  
}

The simplest and most straightforward way is to define an INI directive and give it a default value of "Hello world!":

#include "php_ini.h"  
php_ini_begin ()  
    php_ini_entry ("sample4.greeting", "Hello World",  
                                    Php_ini_all, NULL)  
Php_ini_end ()

As you may have guessed, the first two parameters of this macro represent the name of the INI directive and its default value. The third parameter is used to determine whether the engine allows the INI directive to be modified (this will cover the access level issues to be covered later in this chapter). The last parameter is a callback function that will be invoked each time the value of the INI directive changes. You will see the details of this parameter in the section on modifying events.

If you are in the same way as the translator when you encounter the expected inconsistency between the results and the original results, please add a phrase "register_ini_entries ()" in your Minit () function during the test. Call and make sure that the call is executed after the global space is allocated in your minit.

Now that your INI settings are defined, just use them in your greeting function.

Php_function (Sample4_hello_world)  
{  
    const char *greeting = INI_STR ("sample4.greeting");  
    php_printf ("%s\n", greeting);  
}

Be sure to note that the value of char * is engine-owned and must not be modified. Because of this, you define the variables that you use locally to temporarily store the INI settings as a const modifier. Of course, not all INI values are strings; There are other macros that are used to get integers, floating-point types, and Boolean values:

Long lval = Ini_int ("Sample4.intval");  
Double dval = Ini_flt ("Sample4.fltval");  
Zend_bool bval = Ini_bool ("Sample4.boolval");

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 set values:

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 INI directive's name "Sample4.greeting" adds the extension as a prefix so that it does not conflict with other extended-exposed INI directive names. This prefix is not required for private extensions, but it is encouraged to make public extensions to commercial or open source publishing.

Access level

For the INI directive, the start always has a default value. In most cases, the ideal is to keep the default value unchanged; However, these values may need to be modified for certain special environments or for specific actions within the script. As shown in the following table, the value of the INI directive may be modified at the following 3 points:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.