PHP - 应用程序配置文件存储为 - ini、php、sql、cached、php 类、JSON、php 数组?

我正在尝试确定存储应用程序配置设置的最佳方式.有很多选择.

I am trying to decide on the best way to store my applications configuration settings. There are so many options.

我见过的大多数应用程序都使用了一个简单的 require 和一个包含变量的 PHP 文件.那里似乎有更先进的技术.

The majority of applications I have seen have used a simple require and a PHP file that contains variables. There seem to be far more advanced techniques out there.

你用过什么?什么最有效?什么是最安全的?

What have you used? What is most efficient? What is most secure?

推荐答案

你能做的最好的事情是 可能工作的最简单的事情(php变量)并将其包装在一个类中.这样您就可以稍后更改实现,而无需更改任何客户端代码.创建配置类实现的接口,并使客户端代码使用接口方法.如果您稍后决定将配置存储在数据库或 JSON 或其他任何内容中,您可以简单地用新的实现替换现有的实现.确保您的配置类可测试并编写单元测试.

The best thing you can do is the simplest thing that could possibly work (php variables) and wrap it up in a class. That way you can change the implementation later without changing any client code. Create an interface that the configuration class implements and make the client code use the interface methods. If you later decide to store configuration in a database or JSON or whatever, you can simply swap out the existing implementation with a new one. Make sure your configuration class is testable and write unit tests.

相关文章