Editing PHP files for customization via PHP
First of all, I am assuming this is NOT a bad practice due to various popular programs that use this method, such as SMF.
Anyway, so I have this code:
<?php
// main visual config
$cfg['lang'] = 'en';
// paths
$path['admin'] = 'index.php?p=admin';
$path['admin2'] = 'zvfpcms/admin';
$path['root'] = 'zvfpcms';
$path['images'] = 'zvfpcms/img';
$path['css'] = 'zvfpcms/css';
$path['js'] = 'zvfpcms/js';
$path['pages'] = 'zvfpcms/pg';
?>
The thing is, I want the $ cfg variables to be edited directly through the interface.
How will this be achieved? I don't think string swapping would be a good idea, especially when there are so many (or even endless) possibilities for future $ cfg variables I create.
Or, I have to settle for another method ...
Answers are welcome!
a source to share
Pear Config will allow you to easily read and write configuration to / from various sources, including a PHP file / array. You will probably need to move $cfg
to your own file and include it so that other variables, etc. Have not suffered.
You can also do it yourself by using var_export()
and then writing the file, again you will probably need to move the variable to its own file. I think this would create more messy / less readable PHP than the Pear class.
a source to share
either save the settings in the database, or use the standard ini file (key = value pairs);
lang=en
timezone=CET
Read them:
function loadSettings() {
$cfg = array();
foreach (file('settings.ini') as $line) {
list ($key, $value) = explode('=', $line);
$cfg[$key] = $value;
}
return $cfg;
}
File I / O is less efficient than standard database calls, especially if you already have an open database connection.
a source to share
The reason I believe this bad practice is because you have a working system before editing, there is some error in editing (you may not have escaped the single-quoted character) and after editing the whole application it becomes inaccessible because PHP cannot parse a config file, which affects almost everything you do.
The XML suggestion is not bad, as you can handle any errors that occur while reading the XML.
If you have a database (reading this post, it looks like you're not using it, which is good) that would be the best place for data - what is it for. In the world of flat files, reading in a non-PHP file and interpreting it is much better than automatically editing PHP files.
a source to share