summaryrefslogtreecommitdiff
path: root/documentation/ini.html
blob: c5c512bb5a4e05ca8690d0bf9e8a1bbbdb8ce192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<h1>Reading php.ini variables</h1>
<p>
    Reading settings from the php.ini file(s) is just as simple as it
    is to obtain these settings from a regular PHP script. Inside a PHP script 
    you can use the built-in ini_get() function to read settings from the php.ini 
    file, and in your C++ extension you use the Php::ini_get() function.
</p>
<p>
<pre class="language-c++"><code>#include &lt;phpcpp.h&gt;

/**
 *  Simple function that is used to demonstrate how settings from the
 *  php.ini file can be read
 */
void myFunction()
{
    // read in the "output_buffering" variable from the php.ini file
    int output_buffering = Php::ini_get("output_buffering");
    
    // read in the "variables_order" variable
    std::string variables_order = Php::ini_get("variables_order");
}

/**
 *  Switch to C contect so that the get_module() function can be
 *  called by the Zend engine
 */
extern "C" {
    /**
     *  The get_module() startup function
     *  @return void*
     */
    PHPCPP_EXPORT void *get_module() {
        
        // create extension object
        static Php::Extension extension("my_extension", "1.0");
        
        // export one function
        extension.add("myFunction", myFunction);
        
        // return a pointer to the extension object
        return extension;
    }
}</code></pre>
</p>
<p>
    The Php::ini_get() function returns an object that can be assigned to
    strings, integers and floating point numbers. In the above example we have
    used this to assign the settings directly to an integer and a std::string.
</p>
<p>
    You can only retrieve <i>predefined</i> variables from the php.ini. It is thus not 
    possible to call Php::ini_get() with random strings. If you want to use
    your own variables, you must first register them in the get_module()
    function before you can call Php::ini_get() to retrieve the current values.
</p>
<p>
<pre class="language-c++"><code>#include &lt;phpcpp.h&gt;

/**
 *  Simple function that is used to demonstrate how settings from the
 *  php.ini file can be read
 */
void myFunction()
{
    // read in a variable defined for this extension
    int var1 = Php::ini_get("my_extension.var1");
    
    // read in a string variable
    std::string var2 = Php::ini_get("my_extension.var2");
}

/**
 *  Switch to C contect so that the get_module() function can be
 *  called by the Zend engine
 */
extern "C" {
    /**
     *  The get_module() startup function
     *  @return void*
     */
    PHPCPP_EXPORT void *get_module() {
        
        // create extension object
        static Php::Extension extension("my_extension", "1.0");
        
        // export one function
        extension.add("myFunction", myFunction);
        
        // tell the PHP engine that the php.ini variables my_extension.var1
        // and my_extension.var2 are usable
        extension.add(Php::Ini("my_extension.var1", "default-value"));
        extension.add(Php::Ini("my_extension.var2", 12345));
        
        // return a pointer to the extension object
        return extension;
    }
}</code></pre>
</p>