Tow*_*wer 23 php performance config file
在PHP中存储配置数据的最快方法是什么,以便它可以轻松更改(通过PHP)?首先我考虑过使用config.php文件,但我不能用PHP编辑它,至少不是很简单?然后我考虑使用XML文件,但是为每个HTTP请求解析它们是压倒性的.所以,我想到了INI文件,但后来我认为INI文件仅限于int/string值.最后,我得出的结论是JSON编码文件是最好的:
$config['database']['host'] = ...;
$config['another']['something'] = ...;
...
json_encode($config);
Run Code Online (Sandbox Code Playgroud)
由于JSON可以存储数组,因此我可以使用它创建非常复杂的配置,并且它比INI文件解析得更快.
我的问题:我错过了什么或有没有更好的方法来做到这一点?
lee*_*ers 38
对于存储PHP变量,Serialize是比JSON更好的选择.
我喜欢var_export用于保存配置文件,并include用于加载配置信息.这样可以轻松地以编程方式保存配置数据并使数据易于读取/写入:
config.php文件:
return array(
'var1'=> 'value1',
'var2'=> 'value2',
);
Run Code Online (Sandbox Code Playgroud)
test.php的:
$config = include 'config.php';
$config['var2']= 'value3';
file_put_contents('config.php', '<?php return ' . var_export($config, true) . ';');
Run Code Online (Sandbox Code Playgroud)
更新后的config.php现在包含以下内容:
return array(
'var1'=> 'value1',
'var2'=> 'value3',
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14250 次 |
| 最近记录: |