Set PHP constant with INI file value

Asked

Viewed 70 times

0

I have a Config class in which I define the DEBUG constant with true or false value. This constant is accessed in several pages of the system with:

Config::DEBUG;

I would like to set this constant with the value I have in a file . ini that also saves DB settings.

class Config
{
    const DEBUG = true;
}

I would like to define the DEBUG constant as:

$ini = parse_ini_file('config.ini');
$ini['debug'];

1 answer

0


Good afternoon,

There is a way to open the file and read what is inside it, in PHP there is the function parse_ini_file which allows you to read the data of an INI file and return an array with the data obtained, I see a simple and easy example of how to use this method

$config = parse_ini_file('config.ini', true);
define('HOST',$config['DB']['HOST']);
var_dump(HOST);

ini archive

[DB]
HOST=localhost
PASSWORD=senha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.