Reputation: 1772
I have a project of legacy code that I've nearly successfully converted to use an autoloader rather than a bazillion include and require statements.
The only remaining issue that I have is a config file that sets up some global config stuff. As it is now, it's just a straight PHP that loads variables the application requires.
My idea to make this work is to convert it to a class with class constants for the "global" data. Making the class constant calls will trigger the autoloading of the config class and put everything under the control of the autoloader.
My question is whether this is the best way to approach this and if others have done something similar?
So in my application rather than calling something like this to access the config variables.
$GLOBALS['conf']['thingy'];
That would become something like this.
Config::THINGY
Where things is a class const.
class Config {
const THINGY = 'yes, this is it';
}
This approach also gets rid of global data references, which I think is always a good thing.
Upvotes: 2
Views: 1095
Reputation: 19979
You are definitely on the right track. As with anything related to programming, there are 1 million + 1 ways to skin a cat, here a a few options /w some info.
Yaml: Heavily used by frameworks such as Symfony. If you don't use the SF framework, you can easily implement the stand along SF Yaml parser. Supports hierarchies.
JSON: Great way to maintain config options, highly portable. Supports hierarchies.
ini: You can use the built-in parse_ini_file function for loading your config settings, however hierarchies within ini files are difficult to maintain, and only achievable using an arbitrary namespacing convention.
database: You could actually store settings within the database (with a cache layer between permanent store and the app), this is useful if you want to create a web gui for users to change values, without having to push code.
class constants: Nothing wrong with this approach at all, however it can be argued that either of the config options would be a tad 'cleaner', and more portable.
Upvotes: 1