如何存储 php 应用程序的配置——xml 或 ini 或 db

2022-01-03 00:00:00 configuration php

我有一个用 PHP 编写的应用程序,并且有许多可配置的变量.我们正在实施一项功能,用户可以在其中创建配置集并在它们之间轻松切换.我应该如何存储我的配置?作为 XML?在 .ini 文件中?在多个 .ini 文件中?在数据库中?

I've got an app written in PHP, and there are a number of configurable variables. We're implementing a feature where the user can create sets of configurations and easily switch between them. How should I store my configs? as XML? in a .ini file? in multiple .ini files? in a db?

如果我们在路上添加字段,什么将提供最大的灵活性?编码方便吗?

What's going to provide the most flexibility if we add fields down the road? Coding convenience?

如果我使用数据库,我将不得不使用与应用程序主数据库分开的数据库,因为一些不值得深入研究的原因,这让我回避了这一点.(此外,我们使用 .mdb 文件作为我们的数据库.)

If I use a db, I'll have to use a separate one from the app's main one for reasons that aren't worth going into, which has made me shy away from that. (Also, we're using .mdb files for our db.)

我一直沿用 xml 路线,但在使用 SimpleXML 添加和编辑配置时遇到问题.此外,该应用程序必须与 php 5.1.6 兼容,我对某些功能有点紧张.

I've been going down the xml route, but I'm having problems with adding and editing configs using SimpleXML. Also, the app has to be compatible with php 5.1.6, and I'm a little nervous about some of the functionality.

从来没有真正处理过创建自定义 ini 文件....

Never really dealt with creating custom ini files....

一个小的澄清:用户不会接触文件——他们不是技术人员.因此,初始文件的设置将由我们完成,但随后我们正在编写一个工具,将其配置写入我们选择的任何格式.

A small clarification: the users won't touch the files -- they're non-techie. So setting up the initial files will be done by us, but then we're writing a tool that is going to writie out their configuration(s) to whatever format we choose.

推荐答案

将一个 DB 的设置存储在另一个 DB 中没有多大意义,IMO.

Storing the settings for one DB in a different DB does not make much sense, IMO.

我会选择 ini 文件、xml 文件或 php 文件,正如 jmucchiello.每个选项都有其优点和缺点.至于 xml 与 ini,xml 提供了更大的灵活性,但更难以维护(可读性较差).如果 ini 满足您的需求,请坚持使用更简单的解决方案.仅当您需要的数量超过 ini 文件所能提供的数量时,才使用 xml.

I would choose either an ini file, an xml file, or a php file, as proposed by jmucchiello. Each option has it's pros and cons. As for xml versus ini, xml offers more flexibility but is more difficult to maintain (less readable). If your needs are covered by ini, stick with the simpler solution. Use xml only if you need more than ini files can offer you.

对于php的解决方案,我认为可读性介于ini和xml之间,但是保存当前设置会比较困难.尽管我喜欢这种方法,但对于您目前的情况,我不推荐它.

As for the php solution, I would rate the readability somewhere between ini and xml, but it would be more difficult to save the current settings. So as much as I like the approach, for your current scenarion I would not recommend it.

相关文章