Reading php.ini variables
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.
#include <phpcpp.h>
/**
* 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 context 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;
}
}
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.
You can only retrieve predefined 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.
#include <phpcpp.h>
/**
* 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;
}
}