PHP - Application config file stored as - ini,php,sql,cached,php class,JSON,php array?(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.
这篇关于PHP - 应用程序配置文件存储为 - ini、php、sql、cached、php 类、JSON、php 数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP - 应用程序配置文件存储为 - ini、php、sql、cached、php 类、JSON、php 数组?
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01