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!

0


a source to share


6 answers


Well, I just found Unkwntech's answer here ... I'll save it here for reference.



+1


a source


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.

+1


a source


Just do all the settings in your database and rebuild the config file when you change the settings.

0


a source


You don't have any PHP code. Use an XML configuration file instead. Read the XML information, if you want to make changes, change the value in the data structure generated from the XML file and put it back in a new configuration file.

0


a source


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.

0


a source


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.

0


a source







All Articles