I'm working on a PHP class that will read a semi-standard style of config file. I'm stuck at a point on how to expand to a three level deep value.
Config file right now looks like any other config file you've seen, such as PHP's php.ini or Apache's http.conf
Example File
Expand|Select|Wrap|Line Numbers
- # comments begin with a hash symbol
- #
- [section_name]
- setting=value
- multi=value1,value2,value3
- # this is the second section
- [second_section]
- foo=false
- bar=true
Final Goal:
Expand|Select|Wrap|Line Numbers
- $config = new Config('myFile.cfg');
- // outputs "value"
- echo $config->section_name->setting
- // outputs an array of values (not just comma separated)
- echo $config->section_name->multi
- // throws error, cannot override value of foo
- $config->second_section->foo = true
Everything better by example:
I have an app that get's it data source from multiple places that the user selects from the drop down. Those two places are separate mysql database with identical schema, one for "live" production feed, one for "offline" testing feed.
I want to put these sources in a config along with their mysql login WITHOUT making the source name the "section_na me". Reason: I don't want the app to know what those source names are, (e.g. if they change or add a third source, i'd have to change my code)
So to build my drop down, i'd have to read the entire config directory and parse through all the sections (there are others that are not 'sources') and see which ones are sections, then get their names, and display it in the drop down menu for the user to select (BLEGH! ugly!)
I'd like a config layout that simply gives me the source names (that can later be used as a key to get the MySQL login, etc)
something like....
Expand|Select|Wrap|Line Numbers
- $sourceArray = array_keys($config->sources);
- // build <select><options>
- then alter access it with the selected source:
- $sourceSettings = $config->sources->$selectedSource;
- // then VOILA!! we have:
- mysql_connect($sourceSettings->db_user, $soruceSettings->db_pass, etc);
So! Any Ideas for a (standard-style) config file layout my fried brain can't think of?
Da