在没有数据库的情况下存储、更新、检索 PHP 应用程序的设置
我需要能够将 php 应用程序的数据存储在文件中。 我需要能够在没有 PHP 本身之外的任何外部依赖项的情况下执行此操作。
以下是我的要求:
- 设置不会经常更新/添加/删除。 因此,设置的更新不必非常高效。 然而,我确实希望能够通过 PHP 脚本来完成这一切,而不是通过编辑文件。
- 设置将被不断读取,因此设置的读取必须非常高效。
- 设置采用独特的格式,如果我将它们放在数组中,它可能类似于 $Settings["Database"]["AccessSettings"]["Username"]["myDBUsername"];
$Settings["数据库"]["AccessSettings"]["密码"]["myDBPassword"];
我不想像上面提到的那样将设置存储在数组中。 相反,我更喜欢一些访问方法: getConfig("Database","Accesssettings","Username") 将返回 'myDBUsername'。 这样做的原因是我想限制我存储在全局范围内的变量。
获取/检索这些的最佳方式是什么?
执行我认为可能是 xml 文件的层次结构,但我不确定 PHP 访问 xml 文件是什么样的(特别是我需要能够添加、编辑和删除)。 如果应该是 XML,我应该研究哪种类型的 xml 访问。
如果是另一种格式,我想要一些指向正确方向的指针,以了解如何使用该格式。
I need to be able to store data for a php application in a file. I need to be able to do this without any sort of external dependencies other than PHP itself.
Here are the requirements I have:
- Settings will not be updated/added/removed very often. So updating of settings does not have to be very efficient. However, I do want to be able to do this all through a PHP script, not through editing of files.
- Settings will be read constantly, so reading of settings must be very efficient.
- Settings are in a unique format, if I had them in an array it might be something like $Settings["Database"]["AccessSettings"]["Username"]["myDBUsername"];
$Settings["Database"]["AccessSettings"]["Password"]["myDBPassword"];
I would prefer to not have settings stored in arrays like I mentioned above. Instead I would prefer some access methods: getConfig("Database","Accesssettings","Username") would return 'myDBUsername'. The reason for this is I want to limit the variables I am storing in the global scope.
What would the best way of getting/retrieving these be?
Do the the hierarchy I was thinking possibly an xml file, but I wasn't sure what PHP was like for accessing xml files (particularly the fact that I need to be able to add, edit, and remove). If it should be XML what sort of xml access should I look into.
If it is another format, I would like some pointers to the right direction for what to look into for how to use that format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Brian,parse_ini_file 就是您所需要的。
啊,我错过了您将通过 PHP 编辑此内容的要求。
在这种情况下,PHP 本身没有任何东西可以用于此目的。 你必须自己动手。
只需使用 Zend_Config_Ini 。 我知道您说过您不想使用其他任何东西,但是 Zend Framework 的结构允许您使用您需要的任何部分。 Zend_Config 可以单独使用。 您当然可以将这几个类添加到您的项目中,并让它们处理您的 INI 文件解析。
这是使用上面示例的示例:
您可以简单地加载和访问它:
要编辑和保存您的配置,您可以使用以下命令:
编辑
不太确定为什么人们会根据以下内容对此进行否决vog的误导性评论。 通过使用独占锁使其可供多人写入非常简单。 Zend_Config_Writer 使用 file_put_contents 进行写入,它始终支持 LOCK_EX 标志,它专门锁定文件以进行写入。 使用此标志时,不能让多个写入者同时尝试更新文件。
要将此标志与 Zend_Config_Writer 一起使用,如下所示简单:
替代语法:
Brian, parse_ini_file is what you need.
Ah, I missed the requirement that you'd be editing this via PHP.
In that case there is nothing native to PHP that you can use for this purpose. You'd have to roll your own.
You could save yourself a ton of time by simply using Zend_Config_Ini though. I know you state that you don't want to use anything else, but Zend Framework is structured to allow you to use whatever pieces of it you need. Zend_Config can be used on it's own. You can certainly just add these few classes to your project and let them handle your INI file parsing.
Here is an example using your samples above:
You would load and access this as simply as:
To edit and save your config you would use the following:
Edit
Not really sure why people are voting this down based on vog's misguided comments. It is very simple to make this writable by multiple persons by using an exclusive lock. Zend_Config_Writer uses file_put_contents to do it's writing, which has always supported the the LOCK_EX flag, which exclusively locks a file for writing. When using this flag, you cannot have multiple writers attempting to update the file at the same time.
To use this flag with Zend_Config_Writer it's as simple as follows:
An alternate syntax:
如果您不进行手动编辑,请使用序列化数据。
使用 serialize 写入配置:
使用 unserialize:
您可以使用 PHP 魔术函数 __get() 和 __set() 编写一个类来修改和获取此数据的值。
If you're not hand editing, use serialized data.
Writing config using serialize:
Reading config using unserialize:
You could write a class for modifying and getting values for this data using PHP magic functions __get() and __set().
如果您愿意加入库,您可以使用 Spyc 并将您的配置放在一个 YAML 文件。
您还可以使用 PHP 对 SQLite 的支持 这意味着您的数据库只是一个文件,因此不需要数据库服务器。
If you are willing to drop in a library, you could use Spyc and have your configuration in a YAML file.
You could also use PHP's support for SQLite which would mean your database is just a file so no need for a DB server.
DBM 文件可能不是一个坏主意。 PHP 内置了对各种 DBM 文件类型的支持:
http://www .php.net/manual/en/book.dba.php
DBM files may not be a bad idea. PHP has built in support for the various DBM-file varieties:
http://www.php.net/manual/en/book.dba.php