0

Possible Duplicate:
Reading and Writing Configuration Files

Most of my functions depends on settings.

As of now I'm storing my setting values in database.

For example to display ad in a page i'm checking my database whether to display ad or not

I mean like this

$display_ad = 'get value from database';

if ($display_ad) {
echo 'Ad code goes here';
}

This is fine. But the fact is I have more than 100 settings. So I think my databse load will be reduced if I define the value in a settings.php file like

define('DISPLAY_AD', true); 

if (DISPLAY_AD) {
echo 'Ad code goes here';
}

But I'm not sure this is the right way. Is define() is the correct solution. Or is there any better and faster solution available?

Community
  • 1
  • 1
PrivateUser
  • 4,474
  • 12
  • 61
  • 94

4 Answers4

1

Several options, like those mentioned, include .ini files (using parse_ini_file(), etc.), XML (some concoction with SimpleXML perhaps) but I prefer keeping config in native PHP.

The include() construct allows for one to return from the included file. This allows you to:

config.php

return [
    'foo' => [
        'bar' => [
            'qux' => true,
        ],
        'zip' => false,
    ],
];

elsewhere.php

function loadConfig($file) {
    if (!is_file($file)) {
        return false;
    }
    return (array) call_user_func(function() use($file) {
        // I always re-scope for such inclusions, however PHP 5.4 introduced 
        // $this rebinding on closures so it's up to you
        return include($file);
    });
}

$config = loadConfig('config.php');

if ($config['foo']['bar']['qux']) {
    // yeop
}
if ($config['foo']['zip']) {
    // nope
}

Special care needs to be taken, as when you try to dereference a non-existent dimension, PHP will poop on you:

if ($config['i']['am']['not']['here']) { // poop

}

Creating a wrapper class/functions to manage configuration to your needs is reasonably trivial though. You can add support for cascading configuration (a la web.config in the ASP world), caching, etc.

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
0

define() is quite a good way of doing things. An alternative is to define a global array. such as

$config['display_ad']=true;
$config['something_else']='a value';
//...
function doSomething() {
   global $config;
   if ($config['display_ad']) echo 'Ad code goes here';
}

The latter way is what many projects use, such as phpmyadmin, the reason could be, that you cannot define() a non-scalar value, e.g. define('SOME_ARRAY',array('a','b')) is invalid.

Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
  • I've always operated on the premise that if `global` is the answer, you're asking the wrong question, and need to re-evaluate the problem. +1 for getting OP where he needs to go, -1 for `global`. – Dan Lugg Jan 05 '13 at 15:45
  • @Bracketworks I personally use `define()` wherever possible, but I accept, that a **global** configuration might be one of the questions, that can rightfully be answered by a `global` statement. – Eugen Rieck Jan 05 '13 at 15:49
  • 3
    @EugenRieck I object. Rarely is configuration truly `global`, and as an application grows different modules (*or whatever*) will require independent configuration management. Starting global and working backwards to local (*especially in a growing code-base*) is a task I wouldn't give my enemies. – Dan Lugg Jan 05 '13 at 15:51
0

The simplest thing to execute would be an ini file. You create a file that looks like this:

value1 = foo
value2 = bar
value3 = baz

Then, from PHP, you can execute this:

$iniList = get_ini_file("/path/to/ini/file/you/just/made");
if ($iniList['value1'] == 'foo') {
    print "This will print because the value was set from get_ini_file."
}

If you have a lot of similar constants, that's better than dozens of define methods and quicker than database fetches.

0

you can allso make a class like here: php.net

Aivar
  • 2,029
  • 20
  • 18