编辑PHP文件以通过PHP进行配置
首先,我认为由于使用此方法的各种流行软件(例如SMF),这不是不好的做法。
First of all, I am assuming this is NOT bad practice due to various popular software using this method, such as SMF.
无论如何,所以我目前有代码:
Anyway, so I currently 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';
?>
问题是,我希望通过接口直接编辑$ cfg变量。
The thing is, I want the $cfg variable(s) to be edited directly via an interface.
这将如何实现?我认为替换字符串不是一个好主意,尤其是当我将来创建的$ cfg变量存在很多(甚至是无限)的可能性时。
How would this be achieved? I cannot think replacing strings would be a good idea especially when there are a very large number of possibilities (or even infinite) for future $cfg variables I create.
或者,我将不得不选择其他方法吗?
Or, will I have to settle for a different method...
答案表示赞赏!
Pear Config 可让您轻松地从不同来源读取和写入配置,包括PHP文件/数组。不过,您可能需要将 $ cfg
移到其自己的文件中并包括它,以便其他变量等不受影响。
Pear Config will let you read and write configuration easily to/from different sources, including a PHP file/array. You would probably need to move $cfg
into its own file though and include it so that other variables etc. are unaffected.
您也可以使用 var_export()
a>然后覆盖文件,再次可能需要将变量移到其自己的文件中。我认为这会写出比Pear类更混乱,可读性更差的PHP。
You could also do it yourself using var_export()
and then writing over the file, again you would probably need to move the variable into its own file. I think that this would write messier/less readable PHP than the Pear class.