this is more a conceptual question. Consider you have a php framework that runs a generic website.
You can of course tweak the framework behavior with settings, the question is: where is more natural to place these settings?
My framework consists of some functions that helps me doing some tasks (for example cache managment).
In this fw I use a generic variable
$config = array( 'setting1'=>'value1' etc );
And if a function needs it does a global:
function manageCache() {
global $config;
//> perform task with settings from $config
}
Consider the procedural nature of my framework, and the fact someone says global is evil, how would you manage the settings?
Thanks
Edit1: Please don’t tell to use constats, i have tons of settings and i don’t want to make a tons of constant + they must be editable
There’s nothing wrong with your configuration array. It’s quite a common approach. The fallacy
globals are evilis cargo cult programming advise. Please ignore.Now the
$configarray is often easy to use by itself. But you can expand on that. It’s quite simple for example to turn it into an ArrayObject once initialized:This will allow you to access the settings as
$config["setting"]and$config->settingalike. It’s easier on the eyes.If you also want to avoid having to import the array everywhere, because you sometimes need just a single value, then expand with a wrapper function
config("setting")for convenience.Btw, I’m usually using a mix of config array and constants myself. And I made a little management tool for plugins and a settings array in a central
config.php. http://web135.srv3.sysproserver.de/milki.erphesfurt.de./genericplugins/genconfig.html